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)((ImageButt on)sender) .Parent.Bi ndingConta iner);
DataView dv = Table(TFLDesign).DefaultVi ew;
string fileServerName = AppProperties.getAppProper ty("FormLe tterServer ");
string docPath = AppProperties.getAppProper ty("FormLe tterPath") ;
docPath = docPath.Replace("C:\\","") ;
docPath = docPath.Replace("inetpub\\ ftproot\\" ,"");
string file = fileServerName.ToString()+ docPath.To String()+d v[item.Dat aSetIndex] ["uf_id"]+ "."+dv[ite m.DataSetI ndex]["uf_ ext"];
file = file.Replace("\\","/");
Response.Write("<script>wi ndow.open( 'Controls/ Tables/Fil eExchange/ ShowBinary Data.aspx? "+
"conttype=Application%2Fms word&file= "+file.Rep lace(".rtf ",".doc")+ "','"+Func Code+"_bd' ,'resizabl e=yes,tool bar=no,scr ollbars=ye s,location =no');</sc ript>");
}
What should I change to get this to work?
protected void cmdUDFDownload(object sender, ImageClickEventArgs e)
{
DataGridItem item = ((DataGridItem)((ImageButt
DataView dv = Table(TFLDesign).DefaultVi
string fileServerName = AppProperties.getAppProper
string docPath = AppProperties.getAppProper
docPath = docPath.Replace("C:\\","")
docPath = docPath.Replace("inetpub\\
string file = fileServerName.ToString()+
file = file.Replace("\\","/");
Response.Write("<script>wi
"conttype=Application%2Fms
}
What should I change to get this to work?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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
John
ASKER
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.Lengt h);
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.
FileStream fs = File.OpenRead(file);
BinaryReader br = new BinaryReader(fs);
char[] buff = br.ReadChars((int)fs.Lengt
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 :)
ASKER
string fileServerName = AppProperties.getAppProper
string docPath = AppProperties.getAppProper
docPath = docPath.Replace("C:\\","")
docPath = docPath.Replace("inetpub\\
string file = fileServerName.ToString()+
file = file.Replace("\\","/");
Response.Write("<script language=javascript>window
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.