In Web applications, files stored in fields are sometimes required to be displayed. These files are stored in the database as blob. The display is pushed to the client browser. The following is the sample code. The table name is attachments, And the Blob field name is attachment_content.
<% @ Page Language = "C #" enabletheming = "false" validaterequest = "false" stylesheettheme = "" %>
<% @ Import namespace = "system. Data" %>
<% @ Import namespace = "system. Configuration" %>
<% @ Import namespace = "system. Collections" %>
<% @ Import namespace = "system. Data. sqlclient" %>
<% @ Import namespace = "system. Io" %>
<%
Sqlconnection conn;
Sqlcommand comm;
String SID = request. querystring ["Sid"]. tostring ();
Conn = new sqlconnection ();
Comm = new sqlcommand ();
Conn. connectionstring = configurationmanager. connectionstrings ["hs2005connectionstring"]. connectionstring;
Comm. Connection = conn;
Conn. open ();
Comm. commandtext = @ "select attachment_content, file_name, file_type from attachments where attachment_id = '" + Sid + "'";
Sqldatareader SDR = comm. executereader ();
If (SDR. Read ())
{
Response. Clear ();
Response. clearheaders ();
String fn = SDR ["file_name"]. tostring ();
Response. expires = 0;
Response. Buffer = true;
Response. charset = "UTF-8 ";
This. enableviewstate = false;
Response. contentencoding = system. Text. encoding. utf8;
Response. contenttype = SDR ["file_type"]. tostring ();
Response. addheader ("content-disposition", "attachment; filename =" + FN); // change attachment to inline to display it in the browser.
Response. contenttype = SDR ["file_type"]. tostring ();
Response. binarywrite (byte []) SDR ["attachment_content"]);
}
SDR. Close ();
Conn. Close ();
%>
For more information, see orkerel.