
asp.net下载网络文件
发布日期:2021-05-14 04:36:45
浏览次数:17
分类:博客文章
本文共 4370 字,大约阅读时间需要 14 分钟。
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������ ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
private bool DownLoadFile() { // ������������ ��� ������������ string filePath = Server.MapPath( " ~/DownLoad/myFiles " ); string downLoadFile = " test.mkv " ; FileInfo fileName = new FileInfo(filePath + " \\ " + downLoadFile); FileStream fileStream = new FileStream(filePath + " \\ " + downLoadFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); // ��������� BinaryReader binaryReader = new BinaryReader(fileStream); // ������������������ if (fileName.Exists) { try { long startBytes = 0 ; string lastUpdateTimeStamp = File.GetLastWriteTimeUtc(filePath).ToString( " r " ); string encodeData = HttpUtility.UrlEncode(downLoadFile, Encoding.UTF8) + lastUpdateTimeStamp; // ������response��������� Response.Clear(); Response.Buffer = false ; Response.AddHeader( " Accept-Ranges " , " bytes " ); Response.AppendHeader( " ETag " , " \" " + encodeData + " \" " ); Response.AppendHeader( " Last-Modified " , lastUpdateTimeStamp); // ������contenttype Response.ContentType = " application/octet-stream " ; // ��������������������������������������������������������������� Response.AddHeader( " Content-Disposition " , " attachment;filename= " + fileName.Name); Response.AddHeader( " Content-Length " , (fileName.Length - startBytes).ToString()); Response.AddHeader( " Connection " , " Keep-Alive " ); // ������������������ Response.ContentEncoding = Encoding.UTF8; // ������������ binaryReader.BaseStream.Seek(startBytes, SeekOrigin.Begin); // ������������������1024������������������ int maxCount = ( int )Math.Ceiling((fileName.Length - startBytes + 0.0 ) / 1024 ); // ���1024������������������������ int i; for (i = 0 ; i < maxCount && Response.IsClientConnected; i ++ ) { Response.BinaryWrite(binaryReader.ReadBytes( 1024 )); Response.Flush(); } // ��������������������������������������������������������������������� if (i < maxCount) return false ; return true ; } catch { return false ; } finally { // ������������������������������ Response.End(); binaryReader.Close(); fileStream.Close(); } } else { ScriptManager.RegisterStartupScript( this , GetType(), " ��������������� " , " alert('������������������') " , true ); } return false ; }
���������������������������������������������������������
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年05月03日 19时53分11秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
numpy版本问题
2021-05-14
无法打开文件“opencv_world330d.lib”的解决办法
2021-05-14
maven项目通过Eclipse上传到svn上面,再导入到本地出现指定的类找不到的问题
2021-05-14
maven 项目部署到tomcat下 没有class文件
2021-05-14
算法训练 未名湖边的烦恼(递归,递推)
2021-05-14
算法训练 完数(循环,数学知识)
2021-05-14
什么是接口
2021-05-14
2020版nodejs12.18.3安装配置教程
2021-05-14
iview组件库中,Form组件里的Input,无法正确绑定on-enter事件
2021-05-14
记录-基于springboot+vue.js实现的超大文件分片极速上传及流式下载
2021-05-14
JavaScript高级程序设计第四版学习记录-第九章代理与反射
2021-05-14
怎么解决Windows 10文件/文件夹正在使用无法删除
2021-05-14
matlab函数:fix 向0取整
2021-05-14
Allegro中如何消除器件本身Pin间距报错
2021-05-14
linux--练习001-基础类型
2021-05-14
Flask--简介
2021-05-14
Flask模板--过滤器与测试器
2021-05-14
16 python基础-恺撒密码
2021-05-14