472,100 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,100 software developers and data experts.

Error in opening a word document from a web application

Hi,
I have to open a word document from my aspx page.The Page_Load method
contains the following code

System.IO.TextWriter tx = Response.Output;
System.IO.StreamReader sr = new System.IO.StreamReader(this.Path);
Response.ContentType = "application/msword";
tx.Write(sr.ReadToEnd());
sr.Close();

Just to give more information, I have a First.aspx page which contains
a link to the Second.aspx page passing the complete path value. The
Page_Load method of Second.aspx page was shown above.
If I click the link, FileOpen dialog comes up and asks me for open,save
and cancel options. If I click Open, Microsoft Word opens and greets me
with a message: "Microsoft word needs a converter to display this file
correctly".If I click Cancel, then it is asking for me to select an
encoder.
What is the error? Am I doing the right thing in the Page_Load method.
I have MS Office 2003 installed and my IIS version is 5.1

I would be grateful for any help

Jan 23 '07 #1
4 2069
Sunil,

I think you are not writting the word document to the Response object. Pls
have a look at the sample code which i'v done before ...

string name = "your filename.doc";
Response.ClearHeaders();

Response.ClearContent();

Response.ContentType = "application/vnd.ms-word";

Response.AppendHeader( "content-disposition","inline;filename=" + name );

Response.WriteFile(Server.MapPath("name"));
// taking into consideration your word is in the root application folder
Response.End();

The above code worked for me
--
Thanks & Regards,
Mark Nelson
"sunil" wrote:
Hi,
I have to open a word document from my aspx page.The Page_Load method
contains the following code

System.IO.TextWriter tx = Response.Output;
System.IO.StreamReader sr = new System.IO.StreamReader(this.Path);
Response.ContentType = "application/msword";
tx.Write(sr.ReadToEnd());
sr.Close();

Just to give more information, I have a First.aspx page which contains
a link to the Second.aspx page passing the complete path value. The
Page_Load method of Second.aspx page was shown above.
If I click the link, FileOpen dialog comes up and asks me for open,save
and cancel options. If I click Open, Microsoft Word opens and greets me
with a message: "Microsoft word needs a converter to display this file
correctly".If I click Cancel, then it is asking for me to select an
encoder.
What is the error? Am I doing the right thing in the Page_Load method.
I have MS Office 2003 installed and my IIS version is 5.1

I would be grateful for any help

Jan 23 '07 #2

Mark Nelson wrote:
Sunil,

I think you are not writting the word document to the Response object. Pls
have a look at the sample code which i'v done before ...

string name = "your filename.doc";
Response.ClearHeaders();

Response.ClearContent();

Response.ContentType = "application/vnd.ms-word";

Response.AppendHeader( "content-disposition","inline;filename=" + name );

Response.WriteFile(Server.MapPath("name"));
// taking into consideration your word is in the root application folder
Response.End();

The above code worked for me
--
Thanks & Regards,
Mark Nelson

I am writing into the Response object using a text writer

System.IO.TextWriter tx = Response.Output;
System.IO.StreamReader sr = new System.IO.StreamReader(this.Path);
Response.Clear();
Response.ContentType = "application/msword";
tx.Write(sr.ReadToEnd());
sr.Close();

The same thing works for other files like htm,txt files with
Response.ContentType set properly.
The problem using your code is that my file is not on IIS. So I cannot
use Server.Map()
The path that I refer is a complete path to a Word document in the file
system of the local machine
Any suggestions?
Thanks for the response

Jan 23 '07 #3
I am writing into the Response object using a text writer

That might be your problem? A word file isn't a text file, try a
StreamWriter instead.
The problem using your code is that my file is not on IIS. So I cannot
use Server.Map()
Use a normal path then;

Response.WriteFile(@"c:\somefolder\somefile.doc");
Jan 23 '07 #4

Aidy wrote:
I am writing into the Response object using a text writer

That might be your problem? A word file isn't a text file, try a
StreamWriter instead.
The problem using your code is that my file is not on IIS. So I cannot
use Server.Map()

Use a normal path then;

Response.WriteFile(@"c:\somefolder\somefile.doc");
Response.WriteFile worked for me. Initially, I had thought that it
works only for files on the server.
Thanks for such a tip.

Jan 23 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Elizabeth Harmon | last post: by
5 posts views Thread by Cedric | last post: by
3 posts views Thread by Csaba Gabor | last post: by
reply views Thread by =?Utf-8?B?V2FubmFiZQ==?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.