ASP. NET File Download (the save file dialog box is displayed)

Source: Internet
Author: User

I have read many methods

It seems that all of them are converted by response.

 

Code
Public   Void Open ()
{
 
String Fullfilename =   "" ;

// ASP. NET File Download (the save file dialog box is displayed)
// Fileurl is the full name of the file with a path.
/*
String fileurl = URL;
System. Io. fileinfo = new system. Io. fileinfo (fileurl );
Response. Clear ();
Response. addheader ("content-disposition", "attachment; filename =" + server. urlencode (fileinfo. Name. tostring ()));
Response. addheader ("Content-Length", fileinfo. length. tostring ());
Response. contenttype = "application/octet-stream ";
Response. contentencoding = system. Text. encoding. default;
Response. writefile (fileurl );
*/

// No wrong version:
Try
{
String Filename =   " . // Path // name of the book " ;
Filename =   " . // Path // file name. Extension " ;
Fullfilename = Server. mappath (filename );
// Filename -- name of the file to be downloaded
Fileinfo downloadfile =   New Fileinfo (fullfilename );
If (Downloadfile. exists)
{
Response. Clear ();
Response. clearheaders ();
Response. Buffer =   False ;
Response. contenttype =   " Application/octet-stream " ;
Response. appendheader ( " Content-Disposition " , " Attachment; filename = "   + Httputility. urlencode (downloadfile. fullname, system. Text. encoding. ASCII ));
Response. appendheader ( " Content-Length " , Downloadfile. length. tostring ());
Response. writefile (downloadfile. fullname );
Response. Flush ();
Response. End ();
}
Else
{
// File does not exist
}
}
Catch
{
// An exception occurred while opening.
}

}

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.