C # simple download of file code from the server

Source: Internet
Author: User

1. // download transmitfile
Protected void button#click (Object sender, eventargs E)
{
/*
Microsoft provides a new transmitfile method for the response object to solve the problem of using response. binarywrite
When a file with more than Mbit/s is downloaded, The aspnet_wp.exe process cannot be recycled.
The Code is as follows:
*/
Response. contenttype = "application/X-zip-compressed ";
Response. addheader ("content-disposition", "attachment?filename=z.zip ");
String filename = server. mappath ("download/z.zip ");
Response. transmitfile (filename );
}

Ii. // download writefile
Protected void button2_click (Object sender, eventargs E)
{
/*
Using system. IO;

*/
String filename = "asd.txt"; // file name saved by the client
String filepath = server. mappath ("download/aaa.txt"); // path

Fileinfo = new fileinfo (filepath );
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 ("gb2312 ");
Response. writefile (fileinfo. fullname );
Response. Flush ();
Response. End ();
}

Iii. // multipart download of writefile
Protected void button3_click (Object sender, eventargs E)
{
String filename = "aaa.txt"; // file name saved by the client
String filepath = server. mappath ("download/aaa.txt"); // path

System. Io. fileinfo = new system. Io. fileinfo (filepath );

If (fileinfo. exists = true)
{
Const long chunksize = 102400; // 100 k reads only 100 k each time, which can relieve the pressure on the server
Byte [] buffer = new byte [chunksize];

Response. Clear ();
System. Io. filestream istream = system. Io. file. openread (filepath );
Long datalengthtoread = istream. length; // obtain the total size of the downloaded file
Response. contenttype = "application/octet-stream ";
Response. addheader ("content-disposition", "attachment; filename =" + httputility. urlencode (filename ));
While (datalengthtoread> 0 & response. isclientconnected)
{
Int lengthread = istream. Read (buffer, 0, convert. toint32 (chunksize); // read size
Response. outputstream. Write (buffer, 0, lengthread );
Response. Flush ();
Datalengthtoread = datalengthtoread-lengthread;
}
Response. Close ();
}
}

Iv. // stream download
Protected void button4_click (Object sender, eventargs E)
{
String filename = "aaa.txt"; // file name saved by the client
String filepath = server. mappath ("download/aaa.txt"); // path

// Download an object in the form of a streaming
Filestream FS = new filestream (filepath, filemode. Open );
Byte [] bytes = new byte [(INT) fs. Length];
FS. Read (bytes, 0, bytes. Length );
FS. Close ();
Response. contenttype = "application/octet-stream ";
// Notify the browser to download the file instead of opening it
Response. addheader ("content-disposition", "attachment;
Filename = "+ httputility. urlencode (filename,
System. Text. encoding. utf8 ));
Response. binarywrite (bytes );
Response. Flush ();
Response. End ();
}

//----------------------------------------------------------

Public void downloadfile (system. Web. UI. Page webform, string filenamewhenuserdownload, string filebody)
{

Webform. response. clearheaders ();
Webform. response. Clear ();
Webform. response. expires = 0;
Webform. response. Buffer = true;
Webform. response. addheader ("Accept-language", "ZH-tw ");
// 'File name
Webform. response. addheader ("content-disposition", "attachment;
Filename = '"+ system. Web. httputility. urlencode (filenamewhenuserdownload,
System. Text. encoding. utf8) + "'");
Webform. response. contenttype = "application/octet-stream ";
// 'File content
Webform. response. Write (filebody );//-----------
Webform. response. End ();
}

// The above code downloads a dynamically generated text file. If the file already exists in the server-side object path, you can use the following function:

Public void downloadfilebyfilepath (system. Web. UI. Page webform, string filenamewhenuserdownload, string filepath)
{
Webform. response. clearheaders ();
Webform. response. Clear ();
Webform. response. expires = 0;
Webform. response. Buffer = true;
Webform. response. addheader ("Accept-language", "ZH-tw ");
// File name
Webform. response. addheader ("content-disposition", "attachment;
Filename = '"+ system. Web. httputility. urlencode (filenamewhenuserdownload,
System. Text. encoding. utf8) + "'");
Webform. response. contenttype = "application/octet-stream ";
// File Content
Webform. response. Write (system. Io. file. REA} dallbytes (filepath ));//---------
Webform. response. End ();

}

For your security, please only open the URL with reliable source

Cancel website opening

From: http://hi.baidu.com/einyboy/blog/item/915c2937d5f490cda3cc2ba7.html

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.