Sometimes, when you directly copy a file like http: // 193.100.100.56/testwebsolution/webapplication1/test.raraddress to download the file test.rar on the internet, you are notified that you have not logged on or directly jumped to another page, then, you can directly download the file after logging on. 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, which outputs 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.