5

Could anyone please tell me how could I stream a pdf to a new tab browser? I just have the pdf stream on memory and I want when I click over the link to show the PDF in a new tab or window browser. How could I do that? Thank!!!

I have this link:

<a id="hrefPdf" runat="server" href="#" target="_blank"><asp:Literal ID="PdfName" runat="server"></asp:Literal></a>

In my code behind I have this on the onload event:

                Stream pdf= getPdf
                if (pdf != null)
                {
                    SetLinkPDF(pdf);
                }

    private void SetLinkPDF(IFile pdf)
    {
        hrefPdf.href = "MyPDF to the Browser"
        PdfName.Text = pdf.PdfName;      
    }

Someway I have to process the stream pdf (IFile contains Name, Stream,Metadata, etc of the PDF)

What can I do to proccess this and whe I click show the stream at a new browser?

I have another probelm, is not working the OnClientClick="aspnetForm.target='_blank';" timbck2 suggested me. I have to open the file (image or pdf ) in a new window, what could I do? Is not working this. Thank!

Timbbck2 my asp code is:

<asp:LinkButton runat="server" ID="LinkButtonFile" OnClick="LinkButtonFile_Click" OnClientClick="aspnetForm.target = '_blank';"></asp:LinkButton>

Thanks!!!

user2315877
  • 103
  • 1
  • 2
  • 8

4 Answers4

15

I've done this from a file on disk - sending the data from a memory stream shouldn't be that much different. You need to supply two pieces of metadata to the browser, in addition to the data itself. First you need to supply the MIME type of the data (in your case it would be application/pdf), then the size of the data in bytes (integer) in a Content-Length header, then send the data itself.

So in summary (taking into account the comments and what I think you're asking, your markup would look like this:

<asp:LinkButton ID="whatever" runat="server" OnClick="lnkButton_Click" 
  OnClientClick="aspnetForm.target='_blank';">your link text</aspnet:LinkButton>

These few lines of C# code in your code behind should do the trick (more or less):

protected void lnkButton_Click(object sender, EventArgs e)
{
    Response.ClearContent();
    Response.ContentType = "application/pdf";
    Response.AddHeader("Content-Disposition", "inline; filename=" + docName);
    Response.AddHeader("Content-Length", docSize.ToString());
    Response.BinaryWrite((byte[])docStream);
    Response.End();
}
timbck2
  • 1,036
  • 3
  • 15
  • 36
  • Yes but I need to call the pdf to the browser when I click over the link, how to do that? – user2315877 Apr 24 '13 at 15:33
  • `Response.BinaryWrite()` sends the pdf data to the browser. Or maybe I'm completely misunderstanding your question. It isn't 100% clear to me what you're asking. – timbck2 Apr 24 '13 at 15:41
  • I think I have to convert from Stream to Byte[] am I right?? So what would be the link URL for my pdf one it browse? – user2315877 Apr 24 '13 at 16:29
  • Yes, you're correct, the argument for Response.BinaryWrite() needs to be a byte[]. I still don't understand what you're asking about the link URL. I would use an like so: `link text`. Does that help? – timbck2 Apr 24 '13 at 17:51
  • Yes right, I think I have to convert to array of byte, but on my server onclick event what would I have to write, the above code? Is that way? Thanks in advance! – user2315877 Apr 24 '13 at 21:04
  • docStream supposed to be MemoryStream! Any other types of Stream supposed to be convert in MemoryStream first! https://stackoverflow.com/a/7073124/6836199 – Khalid Bin Sarower Nov 17 '22 at 10:58
1

To display pdf directly in the browser you should set content type for the response to application/pdf

Your user should also have some kind of pdf reader installed for this to work.

alex
  • 12,464
  • 3
  • 46
  • 67
  • Whay do you mean with that? Yes but how could I from a link call a new page and send the pdf stream??? I am using webforms. – user2315877 Apr 24 '13 at 14:09
0

Did you try Response.BinaryWrite?? You probably want to set some headers as well.

Jakob Gade
  • 12,319
  • 15
  • 70
  • 118
  • I have some metadata in my stream. Just have to show it when I click over a link, how to do it? – user2315877 Apr 24 '13 at 14:12
  • Show some of the code you've written so far, and try to explain in more detail what you're trying to accomplish. It's not really clear from your question in its current form. – Jakob Gade Apr 24 '13 at 14:16
  • I get from my DB a PDF as a stream, I want to show it in a link (name.pdf) and when I click over the link I want to show the pdf in the browser. – user2315877 Apr 24 '13 at 16:30
0

The top answer suggests converting (casting) a stream to a byte[] first, that will download the file to the server (from whatever stream it is coming from) and then send it to the client.

If you need to provide a Stream as a parameter for some external service (like me for Azure Storage) you can use the following method.

public ActionResult Coupons(string file)
{
    Response.AddHeader("Content-Disposition", "inline; filename=" + file);
    Response.ContentType = "application/pdf";
    AzureStorage.DownloadFile(file, Response.OutputStream);//second parameters asks for a 'Stream', you can use whatever stream you want here, just put it in the Response.OutputStream
    return new EmptyResult();
}

That will Stream the file to the client directly (as far as I know).

CularBytes
  • 9,924
  • 8
  • 76
  • 101