当前位置:网站首页>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
边栏推荐
- haas506 2.0開發教程-高級組件庫-modem.sms(僅支持2.2以上版本)
- 数值计算方法 Chapter7. 计算矩阵的特征值和特征向量
- Golang regular regexp package use -04- use regular replacement (replaceall(), replaceallliteral(), replaceallfunc())
- C language removes line breaks (or other characters) at the end of strings
- 索引——MySQL
- 图解 Google V8 # 17:消息队列:V8是怎么实现回调函数的?
- Day_ 05 smart communication health project - appointment management - appointment settings
- Haas506 2.0 development tutorial - Advanced Component Library -modem SMS (only supports versions above 2.2)
- LeetCode笔记:Weekly Contest 298
- leetcode - 572. 另一棵树的子树
猜你喜欢

Qt 中 QVariant 使用总结

leetcode - 572. 另一棵树的子树

如何实现与FDA保持邮件通信安全加密?
Link of Baidu URL Parameters? Recherche sur le chiffrement et le décryptage des paramètres d'URL (exemple de Code)

记一次GLIB2.14升级GLIB2.18的记录以及其中的步骤原理

Softing dataFEED OPC Suite将西门子PLC数据存储到Oracle数据库中

Steam教育对国内大学生的影响力

综合培养学生脑力思维的少儿编程

业务逻辑安全思路总结

射频基础理论(dB)
随机推荐
Skilled use of slicing operations
Understand how learning JSX works
Illuminate\Support\Collection 去重 unique 列表去重
haas506 2.0开发教程-高级组件库-modem.sim(仅支持2.2以上版本)
Link of Baidu URL parameter? Research on URL parameter encryption and decryption (code example)
haas506 2.0开发教程-高级组件库-modem.net(仅支持2.2以上版本)
Day_ 08 smart health project - mobile terminal development - physical examination appointment
Haas506 2.0 development tutorial -hota (only supports versions above 2.2)
Basic RF theory (DB)
C language removes line breaks (or other characters) at the end of strings
解析创客教育中的造物原理
idea的去除转义的复制粘贴
The softing datafeed OPC suite stores Siemens PLC data in an Oracle Database
C语言 获取秒、毫秒、微妙、纳秒时间戳
haas506 2.0开发教程-高级组件库-modem.voiceCall(仅支持2.2以上版本)
mysql如何将日期转为数字
haas506 2.0開發教程-高級組件庫-modem.sms(僅支持2.2以上版本)
Haas506 2.0 development tutorial -sntp (only versions above 2.2 are supported)
Leetcode notes: Weekly contest 298
Illuminate\support\collection de duplication unique list de duplication