.net下载

1,Http 协议中有专门的指令来告知浏览器, 本次响应的是一个需要下载的文件. 格式如下: Content-Disposition: attachment;filename=filename.ext 以上指令即标记此次响应流是附件,且附件文件名为 filename.ext 注意: (1): 中文文件名需要进行URLEncode编码, 否则在IE 6 下会提示是”无法识别的文件”.
但经实际测试,在Chrome下不进行URLEncode编码, 也能正常显示.
(2): 文件名不能有空格, 否则也会被认为是”无法识别的文件”.
(3): [ASP.Net中] 向响应流中添加该指令必须使用 response.AddHeader() 函数; 使用
response.Header.Add() 则会报错.
下面是一个实现下载文件功能的函数:

复制代码
       /// <summary> /// 使用微软的TransmitFile下载文件 /// </summary> /// <param name="filePath">服务器相对路径</param> public void TransmitFile(string filePath) { try { filePath = Server.MapPath(filePath); if (File.Exists(filePath)) { FileInfo info = new FileInfo(filePath); long fileSize = info.Length; HttpContext.Current.Response.Clear(); //指定Http Mime格式为压缩包 HttpContext.Current.Response.ContentType = "application/x-zip-compressed"; // Http 协议中有专门的指令来告知浏览器, 本次响应的是一个需要下载的文件. 格式如下: // Content-Disposition: attachment;filename=filename.txt HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(info.FullName)); //不指明Content-Length用Flush的话不会显示下载进度 HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString()); HttpContext.Current.Response.TransmitFile(filePath, 0, fileSize); HttpContext.Current.Response.Flush(); } } catch { } finally { HttpContext.Current.Response.Close(); } }
复制代码

2 下面是使用WriteFile实现下载

复制代码
        /// <summary> /// 使用WriteFile下载文件 /// </summary> /// <param name="filePath">相对路径</param> public void WriteFile(string filePath) { try { filePath = Server.MapPath(filePath); if (File.Exists(filePath)) { FileInfo info = new FileInfo(filePath); long fileSize = info.Length; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.AddHeader("Content-Disposition", "attachement;filename=" + Server.UrlEncode(info.FullName)); //指定文件大小 HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString()); HttpContext.Current.Response.WriteFile(filePath, 0, fileSize); HttpContext.Current.Response.Flush(); } } catch { } finally { HttpContext.Current.Response.Close(); } }
复制代码

3,下面是分块实现下载:

复制代码
        /// <summary> /// 使用OutputStream.Write分块下载文件 /// </summary> /// <param name="filePath"></param> public void WriteFileBlock(string filePath) { filePath = Server.MapPath(filePath); if (!File.Exists(filePath)) { return; } FileInfo info = new FileInfo(filePath); //指定块大小 long chunkSize = 4096; //建立一个4K的缓冲区 byte[] buffer = new byte[chunkSize]; //剩余的字节数 long dataToRead = 0; FileStream stream = null; try { //打开文件 stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); dataToRead = stream.Length; //添加Http头 HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.AddHeader("Content-Disposition", "attachement;filename=" + Server.UrlEncode(info.FullName)); HttpContext.Current.Response.AddHeader("Content-Length", dataToRead.ToString()); while (dataToRead > 0) { if (HttpContext.Current.Response.IsClientConnected) { int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize)); HttpContext.Current.Response.OutputStream.Write(buffer, 0, length); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.Clear(); dataToRead -= length; } else { //防止client失去连接 dataToRead = -1; } } } catch (Exception ex) { HttpContext.Current.Response.Write("Error:" + ex.Message); } finally { if (stream != null) { stream.Close(); } HttpContext.Current.Response.Close(); } }

 public void DownLoadFile(string filePath, bool isUrl)     {         string fileName = "aaa.txt";//客户端保存的文件名

 
   

        if (isUrl)         {             try             {                 filePath = Server.MapPath(filePath);             }             catch { }         }

 
   

        System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);         fileName = fileInfo.Name;         if (fileInfo.Exists == true)         {             string strContentType = "";             string fileExtension = fileInfo.Extension.ToLower();             switch (fileExtension)             {                 case ".mp3":                     strContentType = "audio/mpeg3";                     break;                 case ".mpeg":                     strContentType = "video/mpeg";                     break;                 case ".jpg":                     strContentType = "image/jpeg";                     break;                 case ".bmp":                     strContentType = "image/bmp";                     break;                 case ".gif":                     strContentType = "image/gif";                     break;                 case ".doc":                     strContentType = "application/msword";                     break;                 case ".docx":                     strContentType = "application/msword";                     break;                 case ".ppt":                     strContentType = "application/vnd.ms-powerpoint";                     break;                 case ".pptx":                     strContentType = "application/vnd.ms-powerpoint";                     break;                 case ".xls":                     strContentType = "application/vnd.ms-excel";                     break;                 case ".xlsx":                     strContentType = "application/vnd.ms-excel";                     break;                 case ".pdf":                     strContentType = "application/pdf";                     break;                 case ".css":                     strContentType = "text/css";                     break;                 case ".html":                     strContentType = "text/html";                     break;                 case ".htm":                     strContentType = "text/html";                     break;                 case ".swf":                     strContentType = "application/x-shockwave-flash";                     break;                 case ".exe":                     strContentType = "application/octet-stream";                     break;                 case ".inf":                     strContentType = "application/x-texinfo";                     break;                 default:                     strContentType = "application/octet-stream";                     break;             }

 
   

            const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力             byte[] buffer = new byte[ChunkSize];

 
   

            Response.Clear();             Response.ClearHeaders();             System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);             long dataLengthToRead = iStream.Length;//获取下载的文件总大小             Response.Charset = "utf-8";             Response.ContentEncoding = System.Text.Encoding.UTF8;

 
   

            Response.ContentType = strContentType;             Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8));             while (dataLengthToRead > 0 && Response.IsClientConnected)             {                 int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小                 Response.OutputStream.Write(buffer, 0, lengthRead);                 Response.Flush();                 dataLengthToRead = dataLengthToRead - lengthRead;             }             Response.End();         }     }

 
  

你可能感兴趣的:(.net)