[Add to favorites] preventing leeching download problems

Source: Internet
Author: User

When you copy a file similar to http: // 193.100.100.56/testwebsolution/webapplication1/test.raraddress folder to download the file test.rar, but was told not to log on or directly jump to other pages, and then directly download the file after login. To implement the above situation, it is easier in the. NET world.

1. First create a class library project classlibrary1 to implement the following (Click here to view ):

Using system;

Using system. Web; // reference the system. Web Component

 

Namespace classlibrary1

{

Public class myhandler: ihttphandler

{

Public myhandler ()

{

}

 

# Region ihttphandler Member

Public void processrequest (httpcontext context)

{

// Jump to webform1.aspx and output the rarfile by webform1.aspx

Httpresponse response = context. response;

Response. Redirect ("http: // 193.100.100.56/testwebsolution/webapplication1/webform1.aspx ");

}

 

Public bool isreusable

{

Get

{

// Todo: Add myhandler. isreusable getter implementation

Return true;

}

}

# Endregion

}

}
 

 

2. Create a web project webapplication1 for testing. Add the following nodes to the Web. config file node of the configuration file:

<Httphandlers>

<Add verb = "*" Path = "*. rar" type = "classlibrary1.myhandler, classlibrary1"/>

Httphandlers>

 

3. Add a "Download" button in webform1.aspx. The click event is as follows (Click here to view details ):

Fileinfo file = new system. Io. fileinfo (@ "G: \ webcenter \ testwebsolution \ webapplication1 \ test.rar ");

// The fileinfo class is in the system. Io namespace.

Response. Clear ();

Response. addheader ("content-disposition", "filename =" + file. Name );

Response. addheader ("Content-Length", file. length. tostring ());

String fileextension = file. extension;

 

// Specify the object MIME type based on the file suffix

Switch (fileextension)

{

Case ". MP3 ":

Response. contenttype = "audio/mpeg3 ";

Break;

Case "MPEG ":

Response. contenttype = "Video/MPEG ";

Break;

Case "jpg ":

Response. contenttype = "image/JPEG ";

Break;

Case "...... and so on ":

Response. contenttype = "....";

Break;

Default:

Response. contenttype = "application/octet-stream ";

Break;

}

 

Response. writefile (file. fullname );

Response. End ();
 

 

 

4. Add an application to IIS.ProgramExtension. Choose "default website"> "properties"> "main directory"> "configuration ". In the displayed "application configuration" window, click "add". In the displayed "Add/edit application extension ing" window, select "executable file" C: \ windows \ Microsoft. net \ framework \ v1.1.4322 \ aspnet_isapi.dll, enter ". rar" in the extended name, and click "OK.

 

5. In IE, enter http: // 193.100.100.56/testwebsolution/webapplication1/test.rar to jump to http: // 193.100.100.56/testwebsolution/webapplication1/restart.

 

6. Of course, here We only provide an example to illustrate how to extend it based on your own situation. The following are some reference resources:Article:

L http://www.9seek.com/news/show.aspx? Id = 745 & cid = 12

L http://www.9seek.com/news/show.aspx? Id = 521 & cid = 12

L http://www.9seek.com/news/show.aspx? Id = 520 & cid = 12

L http://msdn.microsoft.com/asp.net/using/building/web/default.aspx? Pull =/library/en-US/dnaspp/html/urlrewriting. asp

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.