ASP. NET 2.0 File Download (support for large files and anti-leech)

Source: Internet
Author: User
1. File Download
There are two main methods to download HTTP files:

    • Direct URL download, with the advantage of low server resource occupation and high speed. The disadvantage is that files in the data library are not allowed to be downloaded, and files in the format of .html can be opened directly in the browser and cannot be downloaded directly.
    • The binary data stream output method has the following advantages: Accurate Measurement of downloads, anti-leeching, direct download of all file formats rather than opening, and medium non-file data stored in the database can be downloaded as files; the disadvantage is that it occupies many server resources.

The principle of large file download is to cut the file into small data streams for download. Microsoft msdn provides an example of large file download, but there is a problem with the Chinese file name garbled, just a slight change.CodeIs:

 Protected     Void  Responsefile (  String  Path)
{
System. Io. Stream istream = Null ;
Byte [] Buffer = New Byte [ 10000 ];
Int Length;
Long Datatoread;
String Filename = System. Io. Path. getfilename (PATH );

Try
{
Istream = New System. Io. filestream (path, system. Io. filemode. Open,
System. Io. fileaccess. Read, system. Io. fileshare. Read );
Datatoread = Istream. length;
Response. contenttype = " Application/octet-stream " ;
Response. addheader ( " Content-Disposition " , " Attachment; filename = " +
Httputility. urlencode (filename, system. Text. encoding. utf8 ));
While (Datatoread > 0 )
{
If (Response. isclientconnected)
{
Length = Istream. Read (buffer, 0 , 10000 );
Response. outputstream. Write (buffer, 0 , Length );
Response. Flush ();

Buffer = New Byte [ 10000 ];
Datatoread = Datatoread - Length;
}
Else
{
Datatoread = - 1 ;
}
}
}
Catch (Exception ex)
{
Response. Write ( " An error occurred while downloading the file! " );
}
Finally
{
If (Istream ! = Null )
{
Istream. Close ();
}
}
}

2. Prevent leeching

 Protected     Void  Page_load (  Object  Sender, eventargs E)
{
/* --------------------------------------------------------------*\
* Large file downloads and anti-leech protection *
**
* Tda7264@163.com *
**
* Floating blog Http://xianfen.net *
\*------------------------------------------------------------- */

If (Request. querystring [ " Filename " ] = Null )
{
Invalidredirect ();
}

String Filename = Request. querystring [ " Filename " ];

If (Filename = String . Empty)
{
Invalidredirect ();
}

// Determine whether the configuration file is directly downloaded
String Downdirect = Configurationmanager. deleettings [ " Down " ]. Tolower ();

If (Downdirect = " True " )
{
Updatehits (filename ); // Update downloads
Response. Redirect ( " Upload/ " + Filename );
Return ;
}

String Path = Server. mappath (request. applicationpath + " /Upload/ " + Filename );
String Referrer = String . Empty;

If (Request. urlreferrer ! = Null )
{
Referrer = Request. urlreferrer. tostring (). tolower ();
}

String D = Configurationmanager. deleettings [ " Valid " ]. Tolower ();
String [] Domainname =
Configurationmanager. deleettings [ " Refers " ]. Tolower (). Split ( New Char [] { ' , ' });

// If it is set to prevent leeching, determine whether the access source is legal
If (D = " False " )
{
Foreach ( String S In Domainname)
{
If (Referrer. indexof (S. tolower ()) > 0 )
{
Updatehits (filename ); // Update downloads
Responsefile (PATH );
Return ;
}
}

Invalidredirect ();
}
Else
{
Responsefile (PATH );
}
}

Protected Void Updatehits ( String Filename)
{
// Code for updating the number of file downloads
}

Protected Void Invalidredirect ()
{
String Defaultpage = Configurationmanager. deleettings [ " Defaultredirect " ];

Response. Redirect (defaultpage, True );
}

3. Configuration File
In the configuration file, configure the download method, whether the leeching function is enabled, and the default page address for leeching:

 <  Appsettings  >  
< Add Key = "Down" Value = "False" />
<! -- Direct download? -->
< Add Key = "Valid" Value = "False" />
<! -- Whether leeching is allowed -->
< Add Key = "Refers" Value = "Localhost, google.cn" />
<! -- Multiple allowed access sources are separated by commas (,). -->
< Add Key = "Defaultredirect" Value = "Error.htm" />
<! -- Default redirection page -->
</ Appsettings >

Complete example: Click to 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.