471,066 Members | 1,288 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Response.WriteFile help please

I want to serve a PDF right to a web page (cannot link browser directly
to PDF file). Stumbled across Response.WriteFile this morning. On my
machine (XP Pro) this worked fine:

private void Page_Load(object sender, System.EventArgs e)
{
string filePath = "c:\\somepath\\some.pdf";
Response.ContentType = "Application/pdf";
Response.WriteFile(filePath);
}

Hit the page in Firefox, PDF loads right up. Hit it in IE, nothing.
Just a broken puzzle icon.

Further, one of the developers on my team did the exact same thing and
it doesn't work at all. IE prompts him to save or open, which will save
the PDF (but it opens as a .aspx so he gets PDF gibberish in VS.NET if
he chooses open), and in Firefox he gets a message about the file being
corrupt.

The eventual goal is to do the same with a file on another computer (not
on the web server), so the questions are:

1)Why isn't it working properly in IE?
2)Why would it work on one machine and not another?
3)Any other tips regarding this?

Thank you very much in advance.

Scott.
--

____________________________________________
Scott C. Reynolds - Tales From the SharpSide
http://www.scottcreynolds.com
sc***@scottcreynolds.com

*****Get your SharpSide Swag!******
http://www.cafepress.com/sharpside/
***********************************
Nov 19 '05 #1
8 5617
Use this code to set the filename so it won't be downloaded as an aspx file.

Response.AddHeader("Content-Disposition", "inline;filename=test.pdf")

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"Scott C. Reynolds" <sr*********@noemail.nospam> wrote in message
news:e3****************@TK2MSFTNGP10.phx.gbl...
I want to serve a PDF right to a web page (cannot link browser directly to
PDF file). Stumbled across Response.WriteFile this morning. On my machine
(XP Pro) this worked fine:

private void Page_Load(object sender, System.EventArgs e)
{
string filePath = "c:\\somepath\\some.pdf";
Response.ContentType = "Application/pdf";
Response.WriteFile(filePath);
}

Hit the page in Firefox, PDF loads right up. Hit it in IE, nothing. Just
a broken puzzle icon.

Further, one of the developers on my team did the exact same thing and it
doesn't work at all. IE prompts him to save or open, which will save the
PDF (but it opens as a .aspx so he gets PDF gibberish in VS.NET if he
chooses open), and in Firefox he gets a message about the file being
corrupt.

The eventual goal is to do the same with a file on another computer (not
on the web server), so the questions are:

1)Why isn't it working properly in IE?
2)Why would it work on one machine and not another?
3)Any other tips regarding this?

Thank you very much in advance.

Scott.
--

____________________________________________
Scott C. Reynolds - Tales From the SharpSide
http://www.scottcreynolds.com
sc***@scottcreynolds.com

*****Get your SharpSide Swag!******
http://www.cafepress.com/sharpside/
***********************************

Nov 19 '05 #2
Steve C. Orr [MVP, MCSD] wrote:
Use this code to set the filename so it won't be downloaded as an aspx file.

Response.AddHeader("Content-Disposition", "inline;filename=test.pdf")

doing that makes IE prompt for save/open, if you hit open, it prompts
again for save/cancel. it breaks the functionality in firefox.

Is there a better way, assuming I can't link directly to the PDF?

--

____________________________________________
Scott C. Reynolds - Tales From the SharpSide
http://www.scottcreynolds.com
sc***@scottcreynolds.com

*****Get your SharpSide Swag!******
http://www.cafepress.com/sharpside/
***********************************
Nov 19 '05 #3
Hello Scott,

You will want to make sure that you clear everything before you write out.

[C#]
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-disposition", "inline;filename=test.pdf");

Response.WriteFile(file);
Response.Flush();
Response.End();

I'm doing it exactly this way, and it works no matter where I run it from
(or which browser).

--
Matt Berther
http://www.mattberther.com
Steve C. Orr [MVP, MCSD] wrote:
Use this code to set the filename so it won't be downloaded as an
aspx file.

Response.AddHeader("Content-Disposition", "inline;filename=test.pdf")

doing that makes IE prompt for save/open, if you hit open, it prompts
again for save/cancel. it breaks the functionality in firefox.

Is there a better way, assuming I can't link directly to the PDF?

*****Get your SharpSide Swag!******
http://www.cafepress.com/sharpside/
***********************************


Nov 19 '05 #4
Matt Berther wrote:
Hello Scott,

You will want to make sure that you clear everything before you write
out.

[C#]
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-disposition", "inline;filename=test.pdf");

Response.WriteFile(file);
Response.Flush();
Response.End();

I'm doing it exactly this way, and it works no matter where I run it
from (or which browser).


You should also set the Content-Length header, since some older Acrobat
Reader plug-ins require this.

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 19 '05 #5
On Fri, 28 Jan 2005 13:17:10 -0800 in
microsoft.public.dotnet.framework.aspnet, Matt Berther
<mb******@hotmail.com> wrote:
[C#]
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-disposition", "inline;filename=test.pdf");

Response.WriteFile(file);
Response.Flush();
Response.End();


Hi Matt,

Do you know if this approach works for other content-types as well?
Such as jpegs (image/jpeg) and Excel files (application/excel) etc.

I need to write a 'fileservice' page that will (based on a querystring
parameter) fetch a file from the server and blast it back to the
client. The documents accessed could be any file type imaginable. I
was planning on writing HttpHandlers for the various content types,
but Response.WriteFile sounds like the silver bullet I was looking
for. Any thoughts?

Thanks,
Roger

Nov 19 '05 #6
Hello Roger,

It does. Of course, you'll have to change the ContentType.

I have done exactly what you describe in several solutions, however instead
of WriteFile, I use BinaryWrite to write out a byte array. If you have them
on the filesystem already, using WriteFile will work just fine.

A lot of times, Ill store images in the database and stuff them into the
cache on first load.

--
Matt Berther
http://www.mattberther.com
On Fri, 28 Jan 2005 13:17:10 -0800 in
microsoft.public.dotnet.framework.aspnet, Matt Berther
<mb******@hotmail.com> wrote:
[C#]
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-disposition",
"inline;filename=test.pdf");
Response.WriteFile(file);
Response.Flush();
Response.End();

Hi Matt,

Do you know if this approach works for other content-types as well?
Such as jpegs (image/jpeg) and Excel files (application/excel) etc.

I need to write a 'fileservice' page that will (based on a querystring
parameter) fetch a file from the server and blast it back to the
client. The documents accessed could be any file type imaginable. I
was planning on writing HttpHandlers for the various content types,
but Response.WriteFile sounds like the silver bullet I was looking
for. Any thoughts?

Thanks,
Roger


Nov 19 '05 #7
Matt Berther wrote:
Hello Roger,

It does. Of course, you'll have to change the ContentType.

I have done exactly what you describe in several solutions, however
instead of WriteFile, I use BinaryWrite to write out a byte array. If
you have them on the filesystem already, using WriteFile will work just
fine.

A lot of times, Ill store images in the database and stuff them into the
cache on first load.

--
Matt Berther
http://www.mattberther.com
On Fri, 28 Jan 2005 13:17:10 -0800 in
microsoft.public.dotnet.framework.aspnet, Matt Berther
<mb******@hotmail.com> wrote:
[C#]
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-disposition",
"inline;filename=test.pdf");
Response.WriteFile(file);
Response.Flush();
Response.End();


Hi Matt,

Do you know if this approach works for other content-types as well?
Such as jpegs (image/jpeg) and Excel files (application/excel) etc.

I need to write a 'fileservice' page that will (based on a querystring
parameter) fetch a file from the server and blast it back to the
client. The documents accessed could be any file type imaginable. I
was planning on writing HttpHandlers for the various content types,
but Response.WriteFile sounds like the silver bullet I was looking
for. Any thoughts?

Thanks,
Roger


Okay...I wrestled this all weekend, trying both WriteFile and
BinaryWrite. I couldn't get it to work. It turns out, in IE, if you
are using the full version of Acrobat then it hoses. But if you are
using the reader as the browser plugin then it's fine. So thanks for
the help. I was going crazy on this because it's such simple code.
Just something for the rest of you to be mindful of - Acrobat = bad!

--

____________________________________________
Scott C. Reynolds - Tales From the SharpSide
http://www.scottcreynolds.com
sc***@scottcreynolds.com

*****Get your SharpSide Swag!******
http://www.cafepress.com/sharpside/
***********************************
Nov 19 '05 #8
Hello Scott,

I do know that Adobe is rather buggy, and one thing that I found helped me
was to make the url end in .pdf

For example: the link that calls this code should look something like:

http://localhost/MyTestApp/GetFile.a...ame=report.pdf

For some reason, adding the extension there helps IE and Adobe out.

--
Matt Berther
http://www.mattberther.com
Matt Berther wrote:
Hello Roger,

It does. Of course, you'll have to change the ContentType.

I have done exactly what you describe in several solutions, however
instead of WriteFile, I use BinaryWrite to write out a byte array. If
you have them on the filesystem already, using WriteFile will work
just fine.

A lot of times, Ill store images in the database and stuff them into
the cache on first load.

--
Matt Berther
http://www.mattberther.com
On Fri, 28 Jan 2005 13:17:10 -0800 in
microsoft.public.dotnet.framework.aspnet, Matt Berther
<mb******@hotmail.com> wrote:

[C#]
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-disposition",
"inline;filename=test.pdf");
Response.WriteFile(file);
Response.Flush();
Response.End();
Hi Matt,

Do you know if this approach works for other content-types as well?
Such as jpegs (image/jpeg) and Excel files (application/excel) etc.

I need to write a 'fileservice' page that will (based on a
querystring parameter) fetch a file from the server and blast it
back to the client. The documents accessed could be any file type
imaginable. I was planning on writing HttpHandlers for the various
content types, but Response.WriteFile sounds like the silver bullet
I was looking for. Any thoughts?

Thanks,
Roger

Okay...I wrestled this all weekend, trying both WriteFile and
BinaryWrite. I couldn't get it to work. It turns out, in IE, if you
are using the full version of Acrobat then it hoses. But if you are
using the reader as the browser plugin then it's fine. So thanks for
the help. I was going crazy on this because it's such simple code.
Just something for the rest of you to be mindful of - Acrobat = bad!

*****Get your SharpSide Swag!******
http://www.cafepress.com/sharpside/
***********************************


Nov 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jerry J | last post: by
2 posts views Thread by Carter | last post: by
2 posts views Thread by David Union | last post: by
1 post views Thread by oz | last post: by
4 posts views Thread by david | last post: by
1 post views Thread by Ryan Pedersen | last post: by
3 posts views Thread by Buddy Ackerman | 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.