Solution code for Asp.net download
Last Update:2018-12-08
Source: Internet
Author: User
1. Create a page for download processing, such as download. aspx. There is nothing in it.
2. Add a downloadhandler class that inherits from the ihttphandler interface and can be used to customize HTTP processing. Program Process HTTP requests synchronously.
Public class downloadhandler: ihttphandler
{
Public void processrequest (httpcontext context)
{
Httpresponse response = context. response;
Httprequest request = context. request;
System. Io. Stream istream = NULL;
Byte [] buffer = new byte [10240];
Int length;
Long datatoread;
Try
{
String filename = filehelper. decrypt (request ["FN"]); // obtain the file name through decryption
String filepath = httpcontext. Current. server. mappath ("~ /") +" Files/"+ filename; // path of the file to be downloaded
Istream = new system. Io. filestream (filepath, system. Io. filemode. Open,
System. Io. fileaccess. Read, system. Io. fileshare. Read );
Response. Clear ();
Datatoread = istream. length;
Long p = 0;
If (request. headers ["range"]! = NULL)
{
Response. statuscode = 206;
P = long. parse (request. headers ["range"]. Replace ("bytes =", ""). Replace ("-",""));
}
If (P! = 0)
{
Response. addheader ("content-range", "bytes" + P. tostring () + "-" + (long) (datatoread-1 )). tostring () + "/" + datatoread. tostring ());
}
Response. addheader ("Content-Length", (long) (datatoread-p). tostring ());
Response. contenttype = "application/octet-stream ";
Response. addheader ("content-disposition", "attachment; filename =" + system. web. httputility. urlencode (system. text. encoding. getencoding (1, 65001 ). getbytes (path. getfilename (filename ))));
Istream. Position = P;
Datatoread = datatoread-P;
While (datatoread> 0)
{
If (response. isclientconnected)
{
Length = istream. Read (buffer, 0, 10240 );
Response. outputstream. Write (buffer, 0, length );
Response. Flush ();
Buffer = new byte [10240];
Datatoread = datatoread-length;
}
Else
{
Datatoread =-1;
}
}
}
Catch (exception ex)
{
Response. Write ("error:" + ex. Message );
}
Finally
{
If (istream! = NULL)
{
Istream. Close ();
}
Response. End ();
}
}
Public bool isreusable
{
Get {return true ;}
}
}
3. A file name encryption and decryption problem is involved here. To prevent the file name from being exposed to the status bar, add a filehelper class, Code As follows:
Public class filehelper
{
Public static string encrypt (string filename)
{
Byte [] buffer = httpcontext. Current. Request. contentencoding. getbytes (filename );
Return httputility. urlencode (convert. tobase64string (buffer ));
}
Public static string decrypt (string encryptfilename)
{
Byte [] buffer = convert. frombase64string (encryptfilename );
Return httpcontext. Current. Request. contentencoding. getstring (buffer );
}
}
Use base64 to encrypt and decrypt the file name.
4. On web. config, add the httphandlers node as follows:
<System. Web>
<Httphandlers>
<Add verb = "*" Path = "Download. aspx" type = "downloadhandler"/>
</Httphandlers>
</System. Web>
5. Create An ASPX page to download the file:
The default. aspx code is as follows:
Default. aspx code
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> File Download </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: hyperlink id = "Link" runat = "server" text = "File Download"> </ASP: hyperlink>
</Div>
</Form>
</Body>
</Html>
The default. aspx. CS code is as follows:
Default. aspx. CS code
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
String url = filehelper. Encrypt ("designpattern. chm ");
Link. navigateurl = "~ /Download. aspx? Fn = "+ URL;
}
}