In c #. net, you can use the app_data folder to implement the anti-Leech function for downloading files and image files.
Principle 1: In asp tutorial. net, the app_data folder contains the local data storage of the application. It typically contains data storage in the form of files (such as microsoft access or microsoft SQL server express database tutorials, xml files, text files, and any other files supported by the application. The content of this folder cannot be processed by the asp.net tutorial. That is to say, the viewer cannot directly access this folder. Therefore, we can use this permission feature to implement anti-Leech protection.
Principle 2: Check the access request address. If the address is not authorized, the anti-Leech page is displayed.
Principle 3: the system. io namespace contains the types that allow reading and writing files and data streams, as well as the types that support basic files and directories. The filestream class reads, writes, opens, and closes files on the file system, and operates on other file-related operating system handles. Filestream can buffer input and output to operate large files.
First, place the downloaded file and image file in the app_data folder (we recommend that you create a subfolder to store it separately ).
Write the address verification code in the getfile. aspx file:
String validstr = "qhdedu.net, qhdedu.com ";
String [] domainname = validstr. tolower (). split (new char [] {','});
String referrer = request. urlreferrer. tostring (). tolower ();
Foreach (string strtmp in domainname)
{If (referrer. indexof (strtmp. tolower ()> 0)
{
// Write the download code here
}
Else
{Response. redirect ("error.htm", true);} // The access address is invalid. Turn to the error page.
}
The variable validstr is the allowed access Source. If there are multiple access sources, separate them with commas (,) and split them into arrays named domainname, the access address referrer is compared with the elements in the array. If the access address exists, the address is valid. If the access address does not exist, it is deemed that the address is invalid.
Depending on the server configuration, the iis process may process data or cache the data in the memory. If the file is too large, data will be cached in the memory when the two processes communicate with each other. This may cause a sharp increase in memory usage on the server, and errors may occur due to memory restrictions on the web server. Therefore, we need to divide the data into smaller parts, and then move it to the output stream for download, so as to use the client to obtain the file data.
The following code downloads an object:
System. io. stream istream = null;
Byte [] buffer = new byte [10000];
Int length;
Long datatoread;
String filename = system. io. path. getfilename (downfile );
Istream = new system. io. filestream (downfile, 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 =" + filename );
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 ;}
}
To display an image file, use the following code:
String filename = server. mappath ("app_data" + downfile );
Httpcontext. current. response. contenttype = "image/jpeg ";
Httpcontext. current. response. writefile (filename );
Downfile is the name of the image file stored in the app_data directory or the name of the downloaded file. Of course, developers can add appropriate settings as needed in the code, such as: can steal links or generate random download file names.
You can download a file and display an image by passing certain parameters to getfile. aspx on a specific page. For example, add the following code to default. aspx:
<A href = '<% = "getfile. aspx? Lb = file & downfile = "+ server. urlencode (" edu.rar ") %> '> test download </a>
" alt = "image test"/>
Lb indicates whether to download an object or display an image.
Create another error "error.htm:
<Body>
<B style = "color: red;"> Sorry, leeching is not allowed on this site </B>
<A href = "http://www.zutiai.com" target = "_ blank" title = ""> Back to Home </a>
</Body>