user2315877
user2315877

Reputation: 103

Stream PDF to browser?

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!!!

Upvotes: 5

Views: 30722

Answers (4)

CularBytes
CularBytes

Reputation: 10321

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

Upvotes: 0

timbck2
timbck2

Reputation: 1066

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();
}

Upvotes: 15

Jakob Gade
Jakob Gade

Reputation: 12427

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

Upvotes: 0

alex
alex

Reputation: 12654

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.

Upvotes: 1

Related Questions