Link to home
Start Free TrialLog in
Avatar of NickPrater
NickPrater

asked on

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?
Avatar of NickPrater
NickPrater

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of JohnModig
JohnModig

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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
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.
Good. I'm glad to see that you have found a workaround and that you could have some use from the code I provided :)