Asp.net File Download Processing

Source: Internet
Author: User

Asp.net File Download Processing
Recently, a project requires BS to download files. I used to remember to execute the file directly through the fileopendialog or filesave control when saving the file in C, however, BS does not seem to have a ready-made control to select the control for the download file path, and then finds some relevant information online. There are roughly two download methods.
1. Download the client file and path directly. The Code is as follows:
Try
{

String filename = "file name ";
String filepath = server. mappath ("/"); // path

Fileinfo = new fileinfo (filepath );
If (fileinfo. exists)
{
Response. Clear ();
Response. clearcontent ();
Response. clearheaders ();
Response. addheader ("content-disposition", "attachment; filename =" + filename );
Response. addheader ("Content-Length", fileinfo. length. tostring ());
Response. addheader ("content-transfer-encoding", "binary ");
Response. contenttype = "application/octet-stream ";
Response. contentencoding = system. Text. encoding. getencoding ("UTF-8 ");
Response. writefile (fileinfo. fullname );
Response. Flush ();
Response. End ();
}
Else
{
Alert. Show ("the downloaded file does not exist. Please confirm ");
}
}
Catch
{
Alert. Show ("Download failed ");
}

2. You can directly call <a href to download the file as follows:
Create a label control. When you click the label control, it is displayed on the download link page.
This. label1.text = "<a href =/" + path for File Download + "target = _ blank> download </a> ";
When you click label, link to a specific file for download.

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.