ASP. net2.0 displays the files stored in database fields

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.