public void Downfile (string guid) { var filetransfer = new Filetransfer (); var directorypath = Path.Combine (Initinfo.config_garbagepath, "FileImport", GUID); Filetransfer.chunktransfer (System.Web.HttpContext.Current, Getdownfilepath (DirectoryPath)); HttpContext.Response.Flush (); Delete temporary files // utility.deletedirectory (DirectoryPath); }
public bool Chunktransfer (HttpContext HttpContext, String filepathname) {bool result = false; if (! System.IO.File.Exists (Filepathname)) {httpContext.Response.StatusCode = 404; return false; } long startpostion = 0; Long endpostion = 0; String fileName = Path.getfilename (filepathname); using (FileStream FileStream = new FileStream (filepathname, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (BinaryReader br = new BinaryReader (FileStream)) {Long filelength = Filestream.length; String lastupdatetime = SYSTEM.IO.FILE.GETLASTWRITETIMEUTC (filepathname). ToString ("R"); String eTag = Httputility.urlencode (FileName, Encoding.UTF8) + lastupdatetime;//Retrieve the request header when downloading; if (httpcontext.request.headers["If-range"]! = null) { if (httpcontext.request.headers["If-range"]. Replace ("\" "," ")! = ETag) {//file Modified HttpContext.Response.StatusCode = 412;//preprocessing failed return false; }} httpContext.Response.Clear (); HttpContext.Response.Buffer = false; HttpContext.Response.AddHeader ("accept-ranges", "bytes"); HttpContext.Response.AppendHeader ("ETag", "\" "+ ETag +" \ "); HttpContext.Response.AppendHeader ("last-modified", lastupdatetime);//write the last modified date to the response HTTP Context.Response.ContentType = "Application/octet-stream"; if (HttpContext.Request.UserAgent.IndexOf ("MSIE") >-1 | | (HttpContext.Request.UserAgent.IndexOf ("Like Gecko") >-1)) {filename = httputility.urlencode (filename, Encoding.UTF8). Replace ("+ ","%20 "); } if (HttpContext.Request.UserAgent.ToLower (). IndexOf ("Firefox") >-1) {HttpContext.Response.AddHeader ("Content-dispositio N "," attachment;filename=\ "" + filename + "\" "); } else {HttpContext.Response.AddHeader ("content-disposition" , "attachment;filename=" + filename); } httpContext.Response.AddHeader ("Connection", "keep-alive"); httpContext.Response.ContentEncoding = Encoding.UTF8; if (httpcontext.request.headers["Range"] = null)//CONT. {HttpContext.Response.Sta Tuscode = 206;//continued identity string[] range = httpcontext.request.headers["range"]. Split (new char[] {' = ', '-'}); Startpostion = long. Parse (range[1]);//The number of bytes already downloaded if (startpostion< 0 | | Startpostion >= filelength) {return false; } if (string. IsNullOrEmpty (range[2])//Specify only the request file start {HttpContext.Response.AddHeader ("Co Ntent-range ", String. Format ("bytes {0}-{1}/{2}", Startpostion, FileLength-1, filelength)); HttpContext.Response.AddHeader ("Content-length", (filelength-startpostion). ToString ()); } else//Specifies the request file range {endpostion = long. Parse (range[2]); HttpContext.Response.AddHeader ("Content-range", String. Format ("bytes {0}-{1}/{2}", Startpostion, Endpostion-startpostion-1, filelength)); HttpContext.Response.AddHeader ("Content-length", (endpostion-startpostion). ToString ()); } } else {//non-continuous HttpContext.Response.AddHeader ("Content-range", String. Format ("bytes {0}-{1}/{2}", Startpostion, FileLength-1, filelength)); HttpContext.Response.AddHeader ("Content-length", (filelength-startpostion). ToString ()); } br. Basestream.seek (Startpostion, Seekorigin.begin); Long MaxCount = (long) math.ceiling ((filelength-startpostion + 0.0)/transferbuffer);//block download, the remainder can be divided into the number of blocks for (long i = 0; i < maxCount && httpContext.Response.IsClientConnected; i++) { HttpContext.Response.BinaryWrite (Br. Readbytes (Transferbuffer)); HttpContext.Response.Flush (); if (Transfersleep > 0) {thread.sleep (transfersleep); }} result = true; }} return result; }
. NET download files from the server