Response.WriteFile help please

S

Scott C. Reynolds

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
(e-mail address removed)

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

Steve C. Orr [MVP, MCSD]

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

Scott C. Reynolds

Steve said:
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
(e-mail address removed)

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

Matt Berther

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).
 
J

Joerg Jooss

Matt said:
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,
 
R

Roger Helliwell

[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
 
M

Matt Berther

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
[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
 
S

Scott C. Reynolds

Matt said:
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
[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
(e-mail address removed)

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

Matt Berther

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.aspx?filename=report.pdf

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

--
Matt Berther
http://www.mattberther.com
Matt said:
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

[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/
***********************************
 
Joined
May 29, 2008
Messages
2
Reaction score
0
Has anyone found a definite cause/resolution to the issue with the PDF not opening in Acrobat Pro? Some of my own research shows that response.writefile increases the file size by a few k vs. getting the file from FTP. Using a binary Diff program the program says the binary files are different... Thanks!
 
Joined
May 29, 2008
Messages
2
Reaction score
0
Solution: For the PDF errors... basically you have to make sure when you transmit the file that you're only sending the file data (nothing else in the response buffer is getting sent--which could happen if you have master pages or something like that). You can ensure this is a clean response by adding.
Response.Clear();
Response.ClearHeaders(); and Response.End(); immediately after sending the file

to the code provided above.

actually i would recommend using this:

FileInfo pdfFileInfo = new FileInfo(fullFileName);
long startPos = 0;
long fileSize = pdfFileInfo.Length;
response.ClearHeaders();
response.Clear();
response.ContentType = "application/pdf";
response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", pdfFileInfo.Name));
response.TransmitFile(fullFileName, startPos, fileSize);
response.End();
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top