文件下载的几种方式.docx

上传人:牧羊曲112 文档编号:3560878 上传时间:2023-03-13 格式:DOCX 页数:7 大小:38.25KB
返回 下载 相关 举报
文件下载的几种方式.docx_第1页
第1页 / 共7页
文件下载的几种方式.docx_第2页
第2页 / 共7页
文件下载的几种方式.docx_第3页
第3页 / 共7页
文件下载的几种方式.docx_第4页
第4页 / 共7页
文件下载的几种方式.docx_第5页
第5页 / 共7页
亲,该文档总共7页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《文件下载的几种方式.docx》由会员分享,可在线阅读,更多相关《文件下载的几种方式.docx(7页珍藏版)》请在三一办公上搜索。

1、文件下载的几种方式先把有问题的代码贴出来吧, using System; using System.Data; using System.Configuration; using System.Collections; 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; using System.IO

2、; namespace infoPlatClient.NetDisk public partial class downLoad : Com.DRPENG.Common.WebStruct.BaseForm / / 取得要下载文件的路径 / private string fileRpath get return RequestfileRpath = null ? : RequestfileRpath; / / 取得要下载文件的名称 / protected void Page_Load(object sender, EventArgs e) if (!IsPostBack) this.Downl

3、oadFile; public void DownloadFile Response.ClearHeaders; Response.Clear; Response.Expires = 0; Response.Buffer =true; Response.AddHeader(Accept-Language, zh-tw); string name = System.IO.Path.GetFileName(fileRpath); System.IO.FileStream files = new FileStream(fileRpath, FileMode.Open, FileAccess.Read

4、, FileShare.Read); byte byteFile=null; if (files.Length = 0) byteFile=new byte1; else byteFile = new bytefiles.Length; files.Read(byteFile, 0, (int)byteFile.Length); files.Close; Response.AddHeader(Content-Disposition, attachment;filename= + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8); Re

5、sponse.ContentType = application/octet-stream;charset=gbk; Response.BinaryWrite(byteFile); Response.End; 之前一直用这种下载方式,可是有一次用户上传了一个700Mb的文件时报内存溢出的问题,分析了一下原因,用户的内存只有256M,而下载文件时要创建内存流,导致了内存溢出。 解决方案:1WriteFile分块下载,就是每次下载指定数量的多件; 2通过超链接的方式; lblDownLoad.Text = 下载 下面是四种实现文件下载的方式: using System; using System.

6、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; using System.IO; public partial class _Default : System.Web.UI.Page protected void Page_Loa

7、d(object sender, EventArgs e) /TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) /* 微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite 下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。 代码如下: */ Response.ContentType = application/x-zip-compressed; Response.AddHeader(Content

8、-Disposition, attachment;filename=z.zip); string filename = Server.MapPath(DownLoad/z.zip); Response.TransmitFile(filename); /WriteFile实现下载 protected void Button2_Click(object sender, EventArgs e) /* using System.IO; */ string fileName =asd.txt;/客户端保存的文件名 string filePath=Server.MapPath(DownLoad/aaa.

9、txt);/路径 FileInfo fileInfo = new FileInfo(filePath); Response.Clear; Response.ClearContent; Response.ClearHeaders; Response.AddHeader(Content-Disposition, attachment;filename= + fileName); Response.AddHeader(Content-Length, fileInfo.Length.ToString); Response.AddHeader(Content-Transfer-Encoding, bin

10、ary); Response.ContentType = application/octet-stream; Response.ContentEncoding = System.Text.Encoding.GetEncoding(gb2312); Response.WriteFile(fileInfo.FullName); Response.Flush; Response.End; /WriteFile分块下载 protected void Button3_Click(object sender, EventArgs e) string fileName = aaa.txt;/客户端保存的文件

11、名 string filePath = Server.MapPath(DownLoad/aaa.txt);/路径 System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); if (fileInfo.Exists = true) const long ChunkSize = 102400;/100K 每次读取文件,只读取100K,这样可以缓解服务器的压力 byte buffer = new byteChunkSize; Response.Clear; System.IO.FileStream iStream = System.

12、IO.File.OpenRead(filePath); long dataLengthToRead = iStream.Length;/获取下载的文件总大小 Response.ContentType = application/octet-stream; Response.AddHeader(Content-Disposition, attachment; filename= + HttpUtility.UrlEncode(fileName); while (dataLengthToRead 0 & Response.IsClientConnected) int lengthRead = iS

13、tream.Read(buffer, 0, Convert.ToInt32(ChunkSize);/读取的大小 Response.OutputStream.Write(buffer, 0, lengthRead); Response.Flush; dataLengthToRead = dataLengthToRead - lengthRead; Response.Close; /流方式下载 protected void Button4_Click(object sender, EventArgs e) string fileName = aaa.txt;/客户端保存的文件名 string fi

14、lePath = Server.MapPath(DownLoad/aaa.txt);/路径 /以字符流的形式下载文件 FileStream fs = new FileStream(filePath, FileMode.Open); byte bytes = new byte(int)fs.Length; fs.Read(bytes, 0, bytes.Length); fs.Close; Response.ContentType = application/octet-stream; /通知浏览器下载文件而不是打开 Response.AddHeader(Content-Disposition, attachment; filename= + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8); Response.BinaryWrite(bytes); Response.Flush; Response.End;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号