首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
您找到你想要的搜索结果了吗?
是的
没有找到

asp.net下载文件几种方式

{ /* 微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite 下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。 代码如下: */ Response.ContentType = "application/x-zip-compressed"; Response.AddHeader("Content-Disposition", "attachment;filename=z.zip"); string filename = Server.MapPath("DownLoad/aaa.zip"); Response.TransmitFile(filename); } //WriteFile实现下载 protected void Button2_Click(object sender, EventArgs e) { /* using System.IO; */ string fileName ="aaa.zip";//客户端保存的文件名 string filePath=Server.MapPath("DownLoad/aaa.zip");//路径 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", "binary"); 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.zip";//客户端保存的文件名 string filePath = Server.MapPath("DownLoad/aaa.zip");//路径 System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); if (fileInfo.Exists == true) { const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力 byte[] buffer = new byte[ChunkSize]; Response.Clear(); System.IO.FileStream iStream = System.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 = iStream.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, Eve

02

.net mvc + layui做图片上传(二)—— 使用流上传和下载图片

摘要:上篇文章写到一种上传图片的方法,其中提到那种方法的局限性,就是上传的文件只能保存在本项目目录下,在其他目录中访问不到该文件。这与浏览器的安全性机制有关,浏览器不允许用户用任意的路径访问服务器上的资源,因为这可能造成服务器上其他位置的信息被泄露。浏览器只允许用户用相对路径直接访问本项目路径下的资源。那么,如果A项目要访问B项目上传的文件资源,这就产生问题了。所以这就需要另外一种方法来解决这个问题,那就是通过 流(Stream)的形式上传和下载文件资源。这种方法因为不是通过路径直接访问文件,而是先把文件读取的流中,然后将流中的数据写入到新的文件中,还原需要上传的文件,所以也就不存在上面的问题了。本片博客,着重介绍一下这种方式的实现。

03

消息服务框架使用案例之--大文件上传(断点续传)功能

消息服务框架使用案例之--大文件上传(断点续传)功能 一、分块上传和断点续传原理 在我们的一个产品应用中,客户需要上传大量的文件到服务器,其中不乏很大的视频文件。虽然可以使用FTP这样成熟稳定的工具,但客户表示不会使用FTP工具,并且我们产品也觉得客户从我们软件在切换到FTP用户体验不好,如果做成后台脚本调用FTP上传那么进度信息很难呈现到我们软件上。最终,决定我们自己做文件上传功能。     大文件上传受限于服务器每次处理数据的能力,不能一次传输完成,所以分块上传是必然的了,由于上传时间可能较长,中途可能

05

扫码

添加站长 进交流群

领取专属 10元无门槛券

手把手带您无忧上云

扫码加入开发者社群

相关资讯

热门标签

活动推荐

    运营活动

    活动名称
    广告关闭
    领券