Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I can get pdf to browser , i want it in iframe with out using generic handler



C#
byte[] bytes = (byte[])GetData("SELECT   * FROM [CME_Attachment] where ID = '" + ActID + "'").Rows[0]["Attachment"];

              string name = name.pdf;
              int FileSize = Convert.ToInt32(ddd.Rows[0][4].ToString());
              Response.AddHeader("Content-type", "application/pdf");
              Response.AddHeader("Content-   Disposition", "attachment; filename=" + name);
              Response.OutputStream.Write(bytes, 0, FileSize);
              Response.Flush();
              Response.End();


              iframepdf.Attributes["src"] = ;


What I have tried:

byte[] bytes = (byte[])GetData("SELECT [A_id],[Attachment],[A_name],[A_Ext],[A_size] ,[A_ActivityID],[A_type]" +
              " FROM [CME_Attachment] where A_ActivityID = '" + ActID + "' and A_type = '" + AttID + "'").Rows[0]["Attachment"];

              string name = ddd.Rows[0][2].ToString();
              int FileSize = Convert.ToInt32(ddd.Rows[0][4].ToString());
              //int FileSize = Convert.ToInt32(row["A_size"].ToString());

              Response.AddHeader("Content-type", "application/pdf");
              Response.AddHeader("Content-   Disposition", "attachment; filename=" + name);
              Response.OutputStream.Write(bytes, 0, FileSize);
              Response.Flush();
              Response.End();
              iframepdf.Attributes["src"]
Posted
Updated 7-Mar-17 3:02am
Comments
Richard Deeming 7-Mar-17 8:38am    
And if you want to display a file that's stored in the database, then you have to use a generic handler.

1 solution

You can't send html that contains an iframe and also the content of the iframe in a single response, you need to split this into two requests. You'll need your original page to render out an iframe whose src element points to a page that only returns the PDF, ie all of your Response.* lines. You'll probably need to add the params you need to generate the file on the querystring so you can read them as you're doing now, however note that anyone can view or modify those params so don't do that if they are sensitive in nature or if you want the file access to be short-lived.

The page that returns the pdf doesn't have to be a handler, it can be an aspx page (but you'll need to add a response.clear before you start to write the PDF) but there's no reason for it not to be, it makes your life easier and you have to add a new file to your project anyway.
 
Share this answer
 
Comments
Richard Deeming 7-Mar-17 9:06am    
Technically, if the content is HTML, and you don't care about IE or Edge support[^], you can use the srcdoc attribute[^]. There's even a polyfill[^] for old browsers.

But binary content is definitely not supported. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900