Asp.net file garbled

Source: Internet
Author: User
String strTemp = System. Web. HttpUtility. UrlEncode (strName, System. Text. Encoding. UTF8); // fix file name garbled characters

Protected string strConn = Common. Config. getdeleettingskeyvalue ("DBConnectString ");
Protected System. Data. OleDb. OleDbConnection conn;
Protected System. Data. OleDb. OleDbCommand cmd;
Protected System. Data. OleDb. OleDbDataReader dr;

Private void Download (string field, string id)
{
Try
{
String strCmd = "select * from doc_body where id =" + id;
Conn = new OleDbConnection (strConn );
Cmd = new OleDbCommand (strCmd, conn );
Conn. Open ();
Dr = cmd. ExecuteReader ();
Dr. Read ();
Int nSize = (int) dr ["doc_size"];
String strContentType = (string) dr ["ContentType"];
String strName = (string) dr ["doc_name"];
Byte [] data = (byte []) dr [field];
If (nSize = 0)
{
Message. Text = "<font color = # 0000ff> NO File Download! </Font> ";
}
Else
{
Response. Clear ();
Response. Buffer = true;
// Response. Charset = "UTF-8 ";
// Response. ContentEncoding = System. Text. Encoding. GetEncoding ("UTF-8 ");
// UTF-8, gb2312, big5
Response. ContentType = strContentType;
// Application/ms-excel, application/ms-word, application/ms-txt, application/ms-html, or other browsers can directly support documents
String strTemp = System. Web. HttpUtility. UrlEncode (strName, System. Text. Encoding. UTF8); // fix file name garbled characters
Response. AppendHeader ("content-disposition", "attachment; filename =" + strTemp); // download the attachment
// Response. AppendHeader ("content-disposition", "online; filename =" + strName); // Open online
Response. OutputStream. Write (data, 0, nSize );
}
}
Catch (Exception exp)
{
Common. utility. MessageBox (this, "Download failed! \ N error Message: \ n "+ exp. Message );
}
Finally
{
Conn. Close ();
}
}

Related Article

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.