Response.WriteFile help please

Discussion in 'ASP .Net' started by Scott C. Reynolds, Jan 28, 2005.

  1. 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


    *****Get your SharpSide Swag!******
    http://www.cafepress.com/sharpside/
    ***********************************
    Scott C. Reynolds, Jan 28, 2005
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:e3MoR%...
    >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
    >
    >
    > *****Get your SharpSide Swag!******
    > http://www.cafepress.com/sharpside/
    > ***********************************
    Steve C. Orr [MVP, MCSD], Jan 28, 2005
    #2
    1. Advertising

  3. 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


    *****Get your SharpSide Swag!******
    http://www.cafepress.com/sharpside/
    ***********************************
    Scott C. Reynolds, Jan 28, 2005
    #3
  4. Scott C. Reynolds

    Matt Berther Guest

    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/
    > ***********************************
    Matt Berther, Jan 28, 2005
    #4
  5. Scott C. Reynolds

    Joerg Jooss Guest

    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:
    Joerg Jooss, Jan 29, 2005
    #5
  6. On Fri, 28 Jan 2005 13:17:10 -0800 in
    microsoft.public.dotnet.framework.aspnet, Matt Berther
    <> 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
    Roger Helliwell, Jan 29, 2005
    #6
  7. Scott C. Reynolds

    Matt Berther Guest

    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
    > <> 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
    Matt Berther, Jan 29, 2005
    #7
  8. 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
    >> <> 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


    *****Get your SharpSide Swag!******
    http://www.cafepress.com/sharpside/
    ***********************************
    Scott C. Reynolds, Jan 31, 2005
    #8
  9. Scott C. Reynolds

    Matt Berther Guest

    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 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
    >>> <> 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/
    > ***********************************
    Matt Berther, Jan 31, 2005
    #9
  10. Scott C. Reynolds

    amd3202

    Joined:
    May 29, 2008
    Messages:
    2
    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!
    amd3202, May 29, 2008
    #10
  11. Scott C. Reynolds

    amd3202

    Joined:
    May 29, 2008
    Messages:
    2
    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();
    amd3202, May 29, 2008
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry J
    Replies:
    2
    Views:
    750
    Jerry J
    Jul 20, 2003
  2. Robert Hanson
    Replies:
    3
    Views:
    642
    Steve C. Orr [MVP, MCSD]
    Nov 21, 2003
  3. Earl Teigrob
    Replies:
    0
    Views:
    899
    Earl Teigrob
    Jan 20, 2004
  4. Replies:
    3
    Views:
    5,674
  5. Ryan Pedersen
    Replies:
    1
    Views:
    2,168
    Daniel Fisher\(lennybacon\)
    Jul 12, 2005
Loading...

Share This Page