当前位置:网站首页>asp.net文件下载demo与相关问题的处理
asp.net文件下载demo与相关问题的处理
2022-06-23 05:31:00 【羊毛韭菜】
下载的demo(可显示文件的总大小,并解决了下载完成自动结束下载)
//分块下载
private void downLoadPackage()
{
System.Web.HttpServerUtility server = System.Web.HttpContext.Current.Server;
string fileName = "aaa.apk";
//路径
string filePath = server.MapPath("DownLoadPackage/aaa.apk");
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=" + HttpUtility.UrlEncode(fileName));
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();
}
}
iis上无法下载.apk和.ipa的问题
单击“新建”,建立新的MIME类型;
扩展名是:.apk MIMI类型是:application/vnd.android.package-archive
扩展名是:.ipa MIMI类型是:application/iphone
边栏推荐
- C language removes line breaks (or other characters) at the end of strings
- Haas506 2.0 development tutorial -hota (only supports versions above 2.2)
- idea的去除转义的复制粘贴
- Day_ 05 smart communication health project - appointment management - appointment settings
- Summary of business logic security ideas
- C语言去除字符串尾部的换行(或其他字符)
- 微信小程序 - 全局监听globalData的某个属性变化,例如监听网络状态切换
- JSON to proto
- 射频内容学习
- Leetcode notes: Weekly contest 298
猜你喜欢

Jour 04 projet de santé mentale - gestion des rendez - vous - gestion des forfaits

haas506 2.0开发教程-hota(仅支持2.2以上版本)

Haas 506 2.0 Tutoriel de développement - bibliothèque de composants avancés - modem. SMS (ne prend en charge que les versions supérieures à 2,2)

Network architecture from Wan to sd-wan edge devices

什么是客户体验自动化?

CPU的功能和基本结构

原址 交换
Link of Baidu URL Parameters? Recherche sur le chiffrement et le décryptage des paramètres d'URL (exemple de Code)

C# wpf 通过绑定实现控件动态加载

Day_ 04 smart health project - appointment management - package management
随机推荐
Day_ 08 smart health project - mobile terminal development - physical examination appointment
Data indicators and data analysis models that designers need to understand
QT creator builds osgearth environment (osgqt msvc2017)
leetcode - 572. 另一棵树的子树
中台库存中的实仓与虚仓的业务逻辑设计
LeetCode笔记:Weekly Contest 298
RF content learning
2.17 haas506 2.0开发教程-system(仅支持2.2以上版本)
Machine learning artifact scikit learn minimalist tutorial
Skilled use of slicing operations
Day_ 01 smart communication health project - project overview and environmental construction
直播带货这么火,如何在小程序中实现视频通话及直播互动功能?
Day_ 06 smart health project - mobile terminal development - physical examination appointment
Qt 中 QVariant 使用总结
Topic35——34. Find the first and last positions of elements in a sorted array
Easy EDA #学习笔记09# | ESP32-WROOM-32E模组ESP32-DevKitC-V4开发板 一键下载电路
Day_ 07 smart communication health project FreeMarker
C language stepping on the pit: document coding error, resulting in Base64 Chinese coding error
VS2013 FFMPEG环境配置及常见错误处理
C# wpf 附加属性实现界面上定义装饰器