CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2007
    Posts
    29

    Stream PDF from URL in mail

    Hi!

    I'm trying to stream a pdf from a webservice page (*.asmx). I do not have access to the Response class so I can't use Response.redirect...
    I have the pdf in byte-format but I don't know how to stream the pdf to i.e. acrobat reader.
    The idea is to have a link (http://name.com/Webpage.asmx/ViewPDF?ID=10) in an email where the receiver can click on the link and open the pdf.
    Can anyone help me?
    I don't want the pdf to be a physical file, but a stream available at "click-time"...

    [WebMethod(EnableSession=true, Description="Get specific document")]
    public void ViewPDF()
    {
    // code
    }

    Br,

    Marjoh

  2. #2
    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    762

    Re: Stream PDF from URL in mail

    Web services won't work like you're expecting without some major development. Web services work typically with SOAP which basically involves serializing data into Xml, sending it, deserializing it, serializing the response, sending it and deserializing it.

    If you wanted to add your ViewPDF query string you'd have to setup a rule in IIS to map all ViewPDF query strings back to a component which would then prepare the request and receive the response.

    What I would suggest doing is create a handler (ashx) and you would basically use Response to write the PDF out to the stream. I know you said you don't have access to the Response class but that's your only choice unless you save it as a temporary file then delete it later (which isn't exactly streaming).

    You could then call it via http://www.MyWebsite.com/ViewPDF.ashx?ID=10

  3. #3
    Join Date
    Nov 2007
    Posts
    29

    Re: Stream PDF from URL in mail

    kasracer - you are the man!
    I will check out this solution instead. Thanks a lot, you really saved my day!

  4. #4
    Join Date
    Nov 2007
    Posts
    29

    Re: Stream PDF from URL in mail

    Hmm... still got problems with this. When I call ViewPDF.ashx I'm redirected to the loginpage of the webpage. I have a website where you have to login to be able to acces the underlying pages. Is there no way I can send a link to an underlying page, without being redirected to the loginpage in order to view the pdf?

    Br,

    Martin

  5. #5
    Join Date
    Nov 2007
    Posts
    29

    Re: Stream PDF from URL in mail

    I solved this by adding:
    <location path="ViewPDF.ashx">
    <system.web>
    <authorization>
    <allow users="*"/>
    </authorization>
    </system.web>
    </location>

    in my web.config for the project. I now get "access denied" instead when trying to read the pdf I want to make a stream of...

  6. #6
    Join Date
    Nov 2007
    Posts
    29

    Re: Stream PDF from URL in mail

    This is what my ashx looks like:

    public void ProcessRequest(HttpContext context)
    {
    byte[] Buffer = FileWrap.GetFileBytes(context.Server.MapPath("pdf/test.pdf"));

    string PdfName = "attachment; filename=TEST.pdf";
    context.Response.ClearContent();
    context.Response.ContentType = "application/pdf";
    //context.Response.AddHeader("content-disposition", "test.pdf");
    context.Response.AddHeader("content-disposition", PdfName);
    context.Response.BinaryWrite(Buffer);
    context.Response.End();
    context.Response.Flush();
    }

    Yes! Solved it.
    Had to change the filestream to:
    FileStream fs = new FileStream("C:///path///test.pdf", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None);

    I was hoping that no webpage would show up, but unfortunately I get a webpage AND the pdf. Skipped the "attachment; filename=" and the pdf is opened within the browser window. I would however preffered to just open the pdf without any browser window...
    Last edited by marjoh; November 20th, 2008 at 09:29 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured