ASP. NET directly downloads a file and opens a file

Source: Internet
Author: User

// Download an object
String connstr = system. configuration. configurationsettings. etettings ["connectionstring"];
String str_get_file = "select * From fmfilecont where fileid = 9 ";
Sqlconnection conn = new sqlconnection (connstr );
Sqlcommand mycommand = new sqlcommand (str_get_file, Conn );
Conn. open ();
Sqldatareader DR = mycommand. executereader ();
If (dr. Read ())
{String filename = Dr ["fname"]. tostring ();
Response. Buffer = true;
Response. Clear ();
Response. contenttype = "+ Dr [FTYPE] +"; // Read File Type
Page. response. addheader ("content-disposition", "attachment; filename =" + httputility. urlencode (filename ));
// When the file name to be downloaded is Chinese, add httputility. urlencode
Byte [] file = (byte []) Dr ["Cont"];
// Fields of files stored in the database. The data type is image.
Response. binarywrite (File );
Response. Flush ();
Response. End ();

// Open a file directly
Int id = convert. toint32 (request. querystring ["docid"], 10 );
String connstr = system. configuration. configurationsettings. etettings ["connectionstring"];
String str_get_file = "select * From fmfilecont where fileid =" + ID;
Sqlconnection conn = new sqlconnection (connstr );
Sqlcommand mycommand = new sqlcommand (str_get_file, Conn );
Conn. open ();

Sqldatareader DR = mycommand. executereader ();

If (dr. Read ())
{
Response. contenttype = (string) Dr ["FTYPE"];
Response. outputstream. Write (byte []) Dr ["Cont"], 0, (INT) Dr ["size"]);
}
Else
{
Response. Write ("this file is not available ");
Response. End ();
}

Dr. Close ();
Mycommand. Connection. Close ();

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.