Filedown (File Download class)

Source: Internet
Author: User

Using system;
Using system. IO;
Using system. Threading;
Using system. Web;

/// <Summary>
/// File Download class
/// </Summary>
Public class filedown
{
Public filedown ()
{}

/// <Summary>
/// The parameter is a virtual path.
/// </Summary>
Public static string filenameextension (string filename)
{
Return Path. getextension (mappathfile (filename ));
}

/// <Summary>
/// Obtain the physical address
/// </Summary>
Public static string mappathfile (string filename)
{
Return httpcontext. Current. server. mappath (filename );
}

/// <Summary>
/// Normal download
/// </Summary>
/// <Param name = "FILENAME"> Virtual File Path </param>
Public static void downloadold (string filename)
{
String destfilename = mappathfile (filename );
If (file. exists (destfilename ))
{
Fileinfo Fi = new fileinfo (destfilename );
Httpcontext. Current. response. Clear ();
Httpcontext. Current. response. clearheaders ();
Httpcontext. Current. response. Buffer = false;
Httpcontext. current. response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode (path. getfilename (destfilename), system. text. encoding. utf8 ));
Httpcontext. Current. response. appendheader ("Content-Length", Fi. length. tostring ());
Httpcontext. Current. response. contenttype = "application/octet-stream ";
Httpcontext. Current. response. writefile (destfilename );
Httpcontext. Current. response. Flush ();
Httpcontext. Current. response. End ();
}
}

/// <Summary>
/// Multipart download
/// </Summary>
/// <Param name = "FILENAME"> Virtual File Path </param>
Public static void download (string filename)
{
String filepath = mappathfile (filename );
Long chunksize = 204800; // specify the block size
Byte [] buffer = new byte [chunksize]; // create a K buffer
Long datatoread = 0; // Number of read bytes
Filestream stream = NULL;
Try
{
// Open the file
Stream = new filestream (filepath, filemode. Open, fileaccess. Read, fileshare. Read );
Datatoread = stream. length;

// Add an HTTP Header
Httpcontext. Current. response. contenttype = "application/octet-stream ";
Httpcontext. Current. response. addheader ("content-disposition", "attachement; filename =" + httputility. urlencode (path. getfilename (filepath )));
Httpcontext. Current. response. addheader ("Content-Length", datatoread. tostring ());

While (datatoread> 0)
{
If (httpcontext. Current. response. isclientconnected)
{
Int length = stream. Read (buffer, 0, convert. toint32 (chunksize ));
Httpcontext. Current. response. outputstream. Write (buffer, 0, length );
Httpcontext. Current. response. Flush ();
Httpcontext. Current. response. Clear ();
Datatoread-= length;
}
Else
{
Datatoread =-1; // prevent client from losing connection
}
}
}
Catch (exception ex)
{
Httpcontext. Current. response. Write ("error:" + ex. Message );
}
Finally
{
If (stream! = NULL) stream. Close ();
Httpcontext. Current. response. Close ();
}
}

/// <Summary>
/// Output the hard disk file, which supports large file downloads, resumable data transfer, speed restrictions, and low resource usage.
/// </Summary>
/// <Param name = "_ request"> page. Request object </param>
/// <Param name = "_ response"> page. Response object </param>
/// <Param name = "_ filename"> download file name </param>
/// <Param name = "_ fullpath"> download path with file name </param>
/// <Param name = "_ speed"> Number of bytes allowed to be downloaded per second </param>
/// <Returns> whether the returned result is successful </returns>
//---------------------------------------------------------------------
// Call:
// String fullpath = server. mappath ("count.txt ");
// Responsefile (this. request, this. response, "count.txt", fullpath, 100 );
//---------------------------------------------------------------------
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 = (INT) math. Floor (double) (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 (double) (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;
}
}

 

Filedown (File Download class)

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.