Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on asp.net a web application. I retrieve a list of pdf in the grid view. now I want to show pdf in an iframe and I also retrieve pdf into bytes when clicking on grid view.

What I have tried:

C#
	cmd.Connection = con;
	con.Open();

	using (MySqlDataReader dr = cmd.ExecuteReader())
	{
		dr.Read();
		bytes = (byte[])dr["Data"];
		contentType = dr["ContentType"].ToString();
	}

	con.Close();
}

string base64 = Convert.ToBase64String(bytes, 0, bytes.Length);

string pdfframesrc = "data:Application/pdf;base64,{0}" + base64;

myiframe.Attributes["Src"] = pdfframesrc;
Posted
Updated 4-Mar-20 4:44am
v2
Comments
MadMyche 3-Mar-20 20:42pm    
And what happens? Does it work? Throw an error? Do you get the content you want when debugging?
Member 14192879 3-Mar-20 22:18pm    
no, it's not working, It does not show pdf in an iframe. as well as it is not showing any error
MadMyche 4-Mar-20 7:07am    
What I would do would be to run this in the Debugger, set a Breakpoint, and then step through line by line to see what the values to see which line is causing the problem.

1 solution

Data URLs[^] have a maximum length which varies depending on the browser. For example, in Chrome, the limit is 2Mb. In Opera, the limit is 64Kb.

PDF files can easily exceed that limit.

Your best bet would be to create a generic handler to load the PDF file for a specific row, and point your <iframe> to that handler.
C#
myiframe.Attributes["src"] = ResolveUrl("~/loadPdfFile.ashx?id=" + idOfTheRecordToLoad);
LoadPdfFile.ashx:
C#
public class LoadPdfFileHandler : IHttpHandler
{
    public bool IsResuable => false;
    
    public void ProcessRequest(HttpContext context)
    {
        string id = context.Request.QueryString["id"];
        // TODO: Verify that the user is allowed to view the specified record.
        
        using (var connection = new MySqlConnection("..."))
        using (var command = new MySqlCommand("SELECT Data, ContentType FROM SomeTable WHERE ID = @ID", connection))
        {
            command.Parameters.AddWithValue("@ID", id);
            
            connection.Open();
            using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
            {
                if (!reader.Read())
                {
                    context.Response.StatusCode = 404;
                    return;
                }

                string contentType = (string)dr["ContentType"];
                if (string.IsNullOrEmpty(contentType)) contentType = "application/octet-stream";
                context.Response.ContentType = contentType;
                
                byte[] bytes = (byte[])dr["Data"];
                context.Response.BinaryWrite(bytes);
            }
        }
    }
}
 
Share this answer
 
Comments
Member 14192879 4-Mar-20 20:22pm    
Thank you very much. This solution solved my problem. can we save back the same pdf to the database after some changes in pdf?
Richard Deeming 5-Mar-20 5:42am    
You can't edit a PDF file in the browser. You'll need to let the user download the PDF file, edit it with software installed on their computer, and then upload the updated file again.

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