ASP tutorial. net File Download class
<%@ Page language= "VB" contenttype= "text/html" responseencoding= "gb2312"%>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>asp.net Tutorial File Download class </title>
<body>
<%
' What does the download class mean?
Implementation of the download function?
The Downloaddata method downloads the resource through the URI specified by the address parameter. This method blocks when downloading a resource. To download the resource and continue execution while waiting for the server to respond, use one of the DownloadDataAsync methods.
If the BaseAddress property is not an empty string ("") and the address does not contain an absolute URI, the address must be a relative URI that is combined with baseaddress to constitute the absolute URI of the requested data. If the QueryString property is not an empty string, append it to address.
This method downloads the FTP resource using the RETR command. For HTTP resources, use the Get method.
The following code example requests data from the server and displays the returned data. It assumes that Remoteuri contains the valid URI of the requested data.
private void FileDownload (string fullfilename)
{
FileInfo DownloadFile = new FileInfo (fullfilename);
Response.Clear ();
Response.ClearHeaders ();
Response.Buffer = false;
Response.ContentType = "Application/octet-stream";
Response.appendheader ("Content-disposition", "attachment;filename=" + httputility.urlencode (DownloadFile.FullName , System.Text.Encoding.UTF8));
Response.appendheader ("Content-length", DownloadFile.Length.ToString ());
Response.WriteFile (Downloadfile.fullname);
Response.Flush ();
Response.End ();
}
Call method
String path= Server.MapPath ("fujian/" + file name);//path path to store files for the server
FileDownload (path);
' File Download class method two
Public class Filehelper
{
///<summary
///uses Base64 code to encrypt file names.
///</summary>
///<param name= "filename" > FileName </param>
///< RETURNS></RETURNS>
public static string Encrypt (string FileName)
{
byte[] buffer = HttpContext.Current.Request.ContentEncoding.GetBytes (filename);
return Httputility.urlencode ( convert.tobase64string (buffer));
}
<summary>
Use Base64 code to decrypt the file name.
</summary>
<param name= "Encryptfilename" > FileName </param>
<returns></returns>
public static string Decrypt (String encryptfilename)
{
byte[] buffer = convert.frombase64string (encryptfilename);
return HttpContext.Current.Request.ContentEncoding.GetString (buffer);
}
}
' Method Three
public static bool Responsefile (HttpRequest _request,httpresponse _response,string _filename,string _fullPath, Long _ Speed
{
Try
{
FileStream myFile = new FileStream (_fullpath, FileMode.Open, FileAccess.Read, fileshare.readwrite);
BinaryReader br = new BinaryReader (myFile);
Try
{
_response.addheader ("accept-ranges", "bytes");
_response.buffer = false;
Long filelength = myfile.length;
Long startbytes = 0;
Double pack = 10240; 10K bytes
int sleep = 200; 5 times per second, 5*10k bytes per second
int sleep = (int) Math.floor (1000 * Pack/_speed) + 1;
if (_request.headers["Range"]!= null)
{
_response.statuscode = 206;
string[] Range = _request.headers["range"]. Split (new char[] {' = ', '-'});
Startbytes = Convert.toint64 (range[1]);
}
_response.addheader ("Content-length", (filelength-startbytes). ToString ());
if (startbytes!= 0)
{
Response.AddHeader ("Content-range", String. Format ("bytes {0}-{1}/{2}", Startbytes, FileLength-1, filelength));
}
_response.addheader ("Connection", "keep-alive");
_response.contenttype = "Application/octet-stream";
_response.addheader ("Content-disposition", "attachment;filename=" + httputility.urlencode (_fileName, SYSTEM.TEXT.ENCODING.UTF8));
Br. Basestream.seek (Startbytes, Seekorigin.begin);
int maxcount = (int) math.floor (filelength-startbytes)/pack) + 1;
for (int i = 0; i < Maxcount; i++)
{
if (_response.isclientconnected)
{
_response.binarywrite (Br. Readbytes (int. Parse (Pack. ToString ()));
Thread.Sleep (sleep);
}
Else
{
I=maxcount;
}
}
}
Catch
{
return false;
}
Finally
{
Br. Close ();
Myfile.close ();
}
}
Catch
{
return false;
}
return true;
}
' Invoke method to join in Page_Load
Page.Response.Clear ();
BOOL success = Responsefile (Page.Request, Page.Response, "Destination file name", @ "source file path", 1024000);
if (!success)
Response.Write ("Error downloading file!") ");
Page.Response.End ();
%>
</body>