Open .rtf from asp.net page

I need to open a .rtf file when a user clicks on an image button in a datagrid.  Here is my current code:

protected void cmdUDFDownload(object sender, ImageClickEventArgs e)
            {
                  DataGridItem item = ((DataGridItem)((ImageButton)sender).Parent.BindingContainer);
                  DataView dv = Table(TFLDesign).DefaultView;      
                  
                  string fileServerName = AppProperties.getAppProperty("FormLetterServer");
                  string docPath = AppProperties.getAppProperty("FormLetterPath");
                  docPath = docPath.Replace("C:\\","");
                  docPath = docPath.Replace("inetpub\\ftproot\\","");

                  string file = fileServerName.ToString()+docPath.ToString()+dv[item.DataSetIndex]["uf_id"]+"."+dv[item.DataSetIndex]["uf_ext"];
                  file = file.Replace("\\","/");
                  Response.Write("<script>window.open('Controls/Tables/FileExchange/ShowBinaryData.aspx?"+
                        "conttype=Application%2Fmsword&file="+file.Replace(".rtf",".doc")+"','"+FuncCode+"_bd','resizable=yes,toolbar=no,scrollbars=yes,location=no');</script>");
            }

What should I change to get this to work?
NickPraterAsked:
Who is Participating?
 
JohnModigCommented:
Ýou have to open Word first and then load the document from the program, like this:
-----------------------------

object fileName = docPath;
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;

Word.ApplicationClass oWordApp = new Word.ApplicationClass();

Word.Document oWordDoc = oWordApp.Documents.Open(ref fileName,
                            ref missing,ref readOnly,
                            ref missing, ref missing, ref missing,
                            ref missing, ref missing, ref missing,
                            ref missing, ref missing, ref isVisible,
                            ref missing,ref missing,ref missing);

oWordDoc.Activate();

-----------------------------
To get above code to work, you have to have a COM reference to the Microsoft Word 10.0 Object Library. Do this in your project by right clicking in the solution explorer on References->Add Reference. Click on the COM tab and look for the Microsoft Word 10.0 Object Library. Click Select and OK.

In C#, the Word Document class's Open method signature is defined as Open(ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object). What this means is that in C# the Open method takes 15 required arguments, and each argument must be preceded with the ref keyword and each argument must be of type object. Since the first argument is a file name, normally a String value in Visual Basic. NET, we must declare a variable of type object that holds the C# string value, hence the code:

object fileName = docPath;

Note that you have to specify your docPath before this, as you do in your existing code. Although we only need to use the first argument in the Open method, remember that C# does not allow optional arguments, so we provide the final 14 arguments as variables of type object that hold values of System.Reflection.Missing.Value

Regards,
John
Use a template
0
 
NickPraterAuthor Commented:
Here is my updated code:

                  string fileServerName = AppProperties.getAppProperty("FormLetterServer");
                  string docPath = AppProperties.getAppProperty("FormLetterPath");
                  docPath = docPath.Replace("C:\\","");
                  docPath = docPath.Replace("inetpub\\ftproot\\","");

                  string file = fileServerName.ToString()+docPath.ToString()+dv[item.DataSetIndex]["uf_id"]+"."+dv[item.DataSetIndex]["uf_ext"];
                  file = file.Replace("\\","/");
                  
                  Response.Write("<script language=javascript>window.open('" + file.ToString() + "');</script>");

This will now open the window with the correct address but the file is not handled by word like it should be since it is associated.  Also FYI I am loading this file from an FTP site.
0
 
NickPraterAuthor Commented:
John - thanks so much for your help, I think this solution is going to work for me.  I am however getting an error that states:

The document name or path is not valid. Try one or more of the following: * Check the path to make sure it was typed correctly. * On the File menu, click Open. Search for the file using this dialog box. (ftp://192.168.0.149/files/FormLetters/17)

It seems as if the .rtf is being chopped off the end of the file name even though when I trace the code it seems to be in the object variable.  Could it also be an issue with word opening a file from our FTP?  I'm going to dig some more but if you have any thoughts on this I would love to hear them.

Thanks again.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
JohnModigCommented:
Well, it appears as you are having problem forming a valid path to the document. I assume you have tried: Response.Write(docPath); just to make sure that this variable is the valid path. Also, make sure that it is so BEFORE the code for open Word executes. To make sure that there are no issues opening the file from your FTP, you can open word manually (using windows startbutton) and then click file > open and type/paste in the full document path. If the file opens without any problem, there has to be an issue with how you form the document path. Try the above and let me know.

John
0
 
NickPraterAuthor Commented:
I've read up some more this morning and it appears the way we are trying to open the document is not supported due to security reasons.  I rewrote my code as:

                  FileStream fs = File.OpenRead(file);
                  BinaryReader br = new BinaryReader(fs);
                  char[] buff = br.ReadChars((int)fs.Length);
                  br.Close();
                  fs.Close();

                  Response.ContentType = "Application/msword";
                  Response.Write(buff, 0, buff.Length);
                  Response.End();

Thanks for the help - I can actually use the code you provided for spell checking purposes in another part of the code.
0
 
JohnModigCommented:
Good. I'm glad to see that you have found a workaround and that you could have some use from the code I provided :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.