open msword from a web form

  • Thread starter Thread starter Jason Huang
  • Start date Start date
J

Jason Huang

Hi,

Using C# web form in ASP.Net, how to open a MS Word application?
Thanks for help.

Jason
 
hi,

Very simple, all you have to do is set the correct content-type and send the
file, use this code:

this.Response.Clear();
this.Response.ContentType = "application/octet-stream";
this.Response.Buffer = false;
this.Response.BufferOutput = false;

FileStream fs = new FileStream(Server.MapPath( currentname),
FileMode.Open, FileAccess.Read);
//StreamReader reader = new StreamReader( Server.MapPath( currentname));

Response.AddHeader("Content-Disposition", "attachment;
filename=\""+targetname + "\"");
Response.AddHeader("Content-Length", fs.Length.ToString());
fs.Close();

Response.WriteFile( Server.MapPath( currentname) );


Cheers,
 
Thanks Ignacio,

It works fine. But when I have the FileAccess.Read changed to
FileAccess.ReadWrite, then it gives me an error message:
System.UnauthorizedAccessException.
I am using ASP.Net on my Windows XP sp2.
Any advice will be appreciated.

Jason
 
Jason Huang said:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
It works fine. But when I have the FileAccess.Read changed to
FileAccess.ReadWrite, then it gives me an error message:
System.UnauthorizedAccessException.
I am using ASP.Net on my Windows XP sp2.
Any advice will be appreciated.

That's probably just permissions, however enabling write access to the
file will not do you any good.

You can't write it by this mechanism. It isn't opening Word on the
server, it is sending the content of the file to the user's browser,
where it is opened by Word. It's equivalent to giving the user a url to
download the word document.
 
Thanks Steve,

But, is it possible to write the word document and save it on the server?
given in the ASP.Net environment.


Jason
 
But, is it possible to write the word document and save it on the server?
given in the ASP.Net environment.

Possible, yes. Not a good idea, though. You can automate Word from C#
server-side using COM interop, but you really shouldn't do so.

Here's a Microsoft kb article explaining how to, why not, and pointing
out some alternatives. It's ASP focused, but it also applies to ASP.NET.

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757
 
Back
Top