Error in opening a word document from a web application

S

sunil

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
 
G

Guest

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
 
S

sunil

Mark said:
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

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
 
A

Aidy

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");
 
S

sunil

Aidy said:
That might be your problem? A word file isn't a text file, try a
StreamWriter instead.


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top