Open web page in WORD

G

Guest

Hi,

I have a form page to print on my web application(asp.net). I want to show
the page in WORD. I use that code in asp.net Page_Load event:
Response.Clear();
Response.ContentType = "application/ms-word";
Response.AddHeader("Content-Disposition", "attachment;filename=word.doc");

When I open the page in IE shows a dialog to ask whether I want to open or
save. It is ok. But when I click Open or Save button, I get an error message:
"
Internet Explorer cannot download form.aspx.
Internet Explorer was not able to open this Internet site. The requested
site is either unavailable or cannot be found. Please try again later.
"

What can I do to solve this problem?

Thanks in advance.
 
A

Andy Fish

Senol Akbulak said:
Hi,

I have a form page to print on my web application(asp.net). I want to show
the page in WORD. I use that code in asp.net Page_Load event:
Response.Clear();
Response.ContentType = "application/ms-word";
Response.AddHeader("Content-Disposition", "attachment;filename=word.doc");

When I open the page in IE shows a dialog to ask whether I want to open or
save. It is ok. But when I click Open or Save button, I get an error
message:
"
Internet Explorer cannot download form.aspx.
Internet Explorer was not able to open this Internet site. The requested
site is either unavailable or cannot be found. Please try again later.
"

What can I do to solve this problem?

hmm, it looks as if you are doing the right thing.

I presume you are streaming the binary contents of a word document directly
after setting these headers. Have you checked the document itself is being
sent correctly.

you could also try putting in a content-length header but I wouldn't have
thought this was necessary.
 
G

Guest

My form content is for html. Is the problem that?
Cannot Word open html content?

Andy Fish said:
Senol Akbulak said:
Hi,

I have a form page to print on my web application(asp.net). I want to show
the page in WORD. I use that code in asp.net Page_Load event:
Response.Clear();
Response.ContentType = "application/ms-word";
Response.AddHeader("Content-Disposition", "attachment;filename=word.doc");

When I open the page in IE shows a dialog to ask whether I want to open or
save. It is ok. But when I click Open or Save button, I get an error
message:
"
Internet Explorer cannot download form.aspx.
Internet Explorer was not able to open this Internet site. The requested
site is either unavailable or cannot be found. Please try again later.
"

What can I do to solve this problem?

hmm, it looks as if you are doing the right thing.

I presume you are streaming the binary contents of a word document directly
after setting these headers. Have you checked the document itself is being
sent correctly.

you could also try putting in a content-length header but I wouldn't have
thought this was necessary.
 
A

Andy Fish

hmm,

Well, word can certainly open an HTML file, but I wouldn't be at all
surprised if streaming HTML content to the browser with a content-type of
word didn't work.

Bear in mind that with content type you are supposed to be telling the
browser what type of file you are sending, and the browser is in charge of
deciding which application to use. There is no way in HTTP of saying "here's
some HTML, open it with word".

Andy

Senol Akbulak said:
My form content is for html. Is the problem that?
Cannot Word open html content?
 
S

Steven Cheng[MSFT]

Hi Senol,

I think Andy's suggestions are reasonable. Though WORD can open HTML
document correcdtly. The IE browser will check the mime-type of the
response stream first. If we output an HTML document but set the content
Type as word, that'll cause the problem. So I suggest you consider the
following options:
1. Convert the content to word compatible document (word or normal rtf)

2. Use text/html as the response's content Type. We can use

Response.AddHeader("Content-Disposition", "attachment;filename=xxxx");

to let the document be opened outside, but whether it'll be opened through
Word.exe will depened on the clientside's settings.

Please feel free to post here if you have any further questions. Thanks,

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Hi Steven,

I sorry, I just realized your answer.

I tried your second suggest, but I still get the same error message.
 
S

Steven Cheng[MSFT]

Thanks for your followup Senol,

Well, I'm not sure whether there is something incorrect on your server's
asp.net/iis environment. But is it ok for you to simple use
Response.WriteFile to output an existing word or excel document. For
example:

private void btnZIP_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.ContentType="application/msword";
Response.AddHeader("content-disposition","attachment; filename=mydoc.doc");
Response.WriteFile(Server.MapPath("~/files/mydoc.doc"));
Response.End();
}


If even this not work, I'm fraid there must be some environment specific
problems we need to troubleshooting.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Thank you at all!!!

I found the problem. The problem was that:
########################################
Response.Cache.SetCacheability(HttpCacheability.NoCache);
########################################

After deleting this line, it run successfully.

Thanks.
 
S

Steven Cheng[MSFT]

Thanks for your followup.

I'm glad that everything works well now.
Have a good day!

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
J

John

Hi There!

I followed the same method to open a web form report page in MS word.
How do I automate the Open [to avoid the file download dialog - would
you like to open the file or save it to your disk?]

Your help is greatly appreciated!

John
 
B

Brock Allen

This is a client browser setting. It'd be a security vulnerability if you
could force an app to open on my machine.
 

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,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top