Scenario:
This post is about how to open the DB stored binary files in a ASP.NET form.
Solution:
This can be achieved with the Response of ASP.NET. Response's BinaryWrite function can write the binary content into appropriate content type and ll be opened.
Code Sample:
string fileName = dt.Rows[0]["AttachFileName"].ToString();
byte[] FileContent = (byte[])dt.Rows[0]["AttachFile"];
Response.Clear();
Response.AddHeader("Content-Disposition", "inline;attachment; filename=" + fileName);
Response.AddHeader("Content-Length", FileContent.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(FileContent);
Response.Flush();
From the above sample code, We are fetching the file name and binary file content from the database and assign to string and byte array respectively. We are adding some response Header with Content Description and Content-Length. Here we also need to specify the content type as octet-stream in order to write it as a file. Finally write the byte array content using BinaryWrite of Response method to open the attached document.
Happy .Neting
This post is about how to open the DB stored binary files in a ASP.NET form.
Solution:
This can be achieved with the Response of ASP.NET. Response's BinaryWrite function can write the binary content into appropriate content type and ll be opened.
Code Sample:
string fileName = dt.Rows[0]["AttachFileName"].ToString();
byte[] FileContent = (byte[])dt.Rows[0]["AttachFile"];
Response.Clear();
Response.AddHeader("Content-Disposition", "inline;attachment; filename=" + fileName);
Response.AddHeader("Content-Length", FileContent.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(FileContent);
Response.Flush();
From the above sample code, We are fetching the file name and binary file content from the database and assign to string and byte array respectively. We are adding some response Header with Content Description and Content-Length. Here we also need to specify the content type as octet-stream in order to write it as a file. Finally write the byte array content using BinaryWrite of Response method to open the attached document.
Happy .Neting
No comments:
Post a Comment