Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, Everyone.

I have an issue in convert Varbinary data to WebBrowser. i have upload pdf file in sql server.

Upload Code :
C#
FileInfo f = new FileInfo(ofg.FileName);
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
br.Close();
fs.Close();
if (bytes.Length > 0)
{
        try
        {
        string strQuery = "INSERT INTO DOC_DET (DOC_FILE) VALUES(@DOC_FILE)";
        SqlCommand cmd = new SqlCommand(strQuery);
        cmd.Parameters.Add("@DOC_FILE", SqlDbType.Binary).Value = bytes;
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            ex.Message.ToString();
        }
        finally { con.Close(); }
}


Now I want to retrieve that Varbinary data into WebBrowser Control

C#
string sql_string = "SELECT DOC_FILE FROM DOC_DET where SEQ_NO = 1;
using (SqlCommand cmd = new SqlCommand(sql_string, con))
             {
                 con.Open();
                 Byte[] bytes = (Byte[])cmd.ExecuteScalar();
                 con.Close();

                 if (drv["DOC_TYPE"].ToString() == ".pdf")
                 {
                     MemoryStream stream = new MemoryStream(bytes);
                     webBrowser1.DocumentStream = stream;
                     webBrowser1.Show();
                 }
             }


What I have tried:

I get This type of unicode display in webbrowser control

output code : "%PDF-1.4 %ÿÿÿÿ 34 0 obj <> stream 2017-10-10T08:36:21Z Nitro Pro 7 (7. 0. 2. 8) 2017-10-10T08:36:22Z 2017-10-10T08:36:22Z application/pdf Nitro Pro 7 (7. 0. 2. 8) uuid:4308e8b5-94b6-42d8-b4f7-4b74a08537af endstream endobj 33 0 obj <> endobj 32 0 obj <> stream xÚl»Â0 E÷~…G˜ÂClˆ„ԁ‡¨Äb·ŠDÈIþž¤- ±|}®eGíëCÍ6‚ºŠ3 Eh-£PpI Áƒ:ËÕrhMœªá5½ö•Êææ"õ5·Ö#…ÉO¤ºå¢¼`Ö¸§Åƒ3©'ŽaHm¥.‚$–;˜ÝIP³žô&yÿ¤BÂfPˆqÈËq°qHÁkC¢¹£j»È±ƒí1Ç®°?ýÉõhÿà0VÅõé—åºýIû³î LÉ» Ç©¢–],Ó÷—Þÿÿ*È/é;ÿÿ)jq& endstream endobj 31 0 obj <> stream xÚbÿÿb``h¸ÿÿAýÿý/êïï=ÿÿ> stream xÚ¤ËEPч»»»»ßÿfü lé¢I'þLägGÅBÇ%$¥¤edåä•”UTÕ¾Œº†¦–¶Ž®ž¾¡‘±‰©YxÌ-,­¬mlíì;|ü£“³‹«›»‡g`/oÿÿ²`°d°b°f°òlìì€,G'gÿÿñ™ç endstream endobj 29 0 obj <> stream xÚl[HaÇÿ3»Š†•—¤¥ü†I‹\" i©õAÔ4gIav]u/ë%Q‰RºX£T†‰X„I%]¨O{ñÒÖ Ò ¤· P(¢— ª·Øí̪¥äïr~ÿsÎwÎ×Ör² ±è†ÎÊö6}d,ÀM@®Ô4œk|Ó ˆhÍÔÔwVg5ÝX¿EKz•×_?"mŽ¿¥œÀP(.(³’¿[ohëÈ–c<"

Not Show PDF Proper way.

i am not saving pdf file direct Display file
Posted
Updated 13-Nov-17 12:16pm
v3

1 solution

First issue you have is you are using ExecuteScalar. ExecuteScalar returns the first 2033 characters only - refer to MSDN; SqlCommand.ExecuteScalar Method ()[^]

Best option is to return a Datatable/DataSet that contains your data.
Then the code I use which works in IE & Chrome is as follows;
C#
// get the data from the datatable
byte[] bytFile = (byte[])dataTable.Rows[0][VarbinaryFieldName];
// you need to pass the file extension and MIME type & ContentType - hardcoded for brevity
// note the \ characters are to escape the " quote marks
this.Context.Response.ContentType = "\".pdf\",\"application/pdf\"";
// add Header
this.Context.Response.AddHeader("Content-disposition","attachment; filename=filename.pdf");
this.Context.Response.BinaryWrite(bytFile);
Response.End(); 


Kind Regards
 
Share this answer
 
Comments
RKparmar 14-Nov-17 0:12am    
Thanks for solution.
But in winform Application there is not contentType Property.
so how can i manage this ?

DataRowView drv = dgvDocument.Rows[e.RowIndex].DataBoundItem as DataRowView;
string sql_string = "SELECT DOC_FILE FROM DOC_DET where SEQ_NO = " + drv["SEQ_NO"];
using (SqlCommand cmd = new SqlCommand(sql_string, con))
{
con.Open();

SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
Byte[] bytes = (Byte[])dt.Rows[0]["DOC_FILE"];
con.Close();

if (drv["DOC_TYPE"].ToString() == ".pdf")
{
MemoryStream stream = new MemoryStream(bytes);
webBrowser1.DocumentStream = stream;
webBrowser1.Show();
}
}
an0ther1 14-Nov-17 18:09pm    
From a quick bit of research you cannot do this using Acrobat - it cannot populate a document from a stream. Even if a browser control was used Acrobat never displays from a stream. The stream is saved to Temp Internet Files & then displayed.
Apparently there is a NuGet package that can display a PDF from a stream, do a search for PDFium. I cannot vouch for the functionality nor am I recommending it or endorsing it but it may solve your issue.
Perhaps you need to re-think your design here.

Kind Regards



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