Download
// Method 1
/// <Summary>
/// File Download
/// </Summary>
/// <Param name = "fullfilename"> </param>
Private void filedownload (string fullfilename)
{
Fileinfo downloadfile = new fileinfo (fullfilename );
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. utf8 ));
Response. appendheader ("Content-Length", downloadfile. length. tostring ());
Response. writefile (downloadfile. fullname );
Response. Flush ();
Response. End ();
}
// Method 2
Filestream = new filestream (server. mappath (dataurl), filemode. Open );
Long filesize = filestream. length;
Context. response. contenttype = "application/octet-stream ";
Context. response. addheader ("content-disposition", "attachment; filename = \" "+ httputility. urlencode (filenamesystem. Text. encoding. utf8) + "\"");
Context. response. addheader ("Content-Length", filesize. tostring ());
Byte [] filebuffer = new byte [filesize];
Filestream. Read (filebuffer, 0, (INT) filesize );
Filestream. Close ();
Context. response. binarywrite (filebuffer );
Context. response. End ();
// Method 3
// Output hard disk files, which support large files, resume, speed limit, and small resource usage
// Input parameter _ Request: page. request object, _ response: page. response object, _ filename: Download file name, _ fullpath: Download path with file name, _ speed bytes allowed to be downloaded per second
// Whether the response is successful
Public static bool responsefile (httprequest _ Request, httpresponse _ response, string _ filename, string _ fullpath, long _ speed)
{
Try
{
Filestream myfile = new filestream (_ fullpath, filemode. Open, fileaccess. Read, fileshare. readwrite );
Binaryreader BR = new binaryreader (myfile );
Try
{
_ Response. addheader ("Accept-ranges", "bytes ");
_ Response. Buffer = false;
Long filelength = myfile. length;
Long startbytes = 0;
Int pack = 10240; // 10 K bytes
// Int sleep = 200; // 5 times per second, that is, 5*10 K bytes per second
Int sleep = (INT) math. Floor (1000 * pack/_ speed) + 1;
If (_ request. headers ["range"]! = NULL)
{
_ Response. statuscode = 206;
String [] range = _ request. headers ["range"]. Split (New char [] {'= ','-'});
Startbytes = convert. toint64 (range [1]);
}
_ Response. addheader ("Content-Length", (filelength-startbytes). tostring ());
If (startbytes! = 0)
{
_ Response. addheader ("content-range", String. Format ("bytes {0}-{1}/{2}", startbytes, fileLength-1, filelength ));
}
_ Response. addheader ("connection", "keep-alive ");
_ Response. contenttype = "application/octet-stream ";
_ Response. addheader ("content-disposition", "attachment; filename =" + httputility. urlencode (_ filename, system. Text. encoding. utf8 ));
BR. basestream. Seek (startbytes, seekorigin. Begin );
Int maxcount = (INT) math. Floor (filelength-startbytes)/Pack) + 1;
For (INT I = 0; I <maxcount; I ++)
{
If (_ response. isclientconnected)
{
_ Response. binarywrite (Br. readbytes (pack ));
Thread. Sleep (sleep );
}
Else
{
I = maxcount;
}
}
}
Catch
{
Return false;
}
Finally
{
BR. Close ();
Myfile. Close ();
}
}
Catch
{
Return false;
}
Return true;
}