【网上购物论坛】-IT泡吧![Www.itpob.Cn]网上购物社区! - Powered by Discuz!
http://www.itpob.cn/
ASP.NET文件下载函数的使用使得我们的程序操作有了明显的提升,那么本文就向你介绍ASP.NET文件下载函数的相关信息。
-
Page.Response.Clear();
-
bool success = ResponseFile(Page.Request, Page.Response, "目的文件名称", @"源文件路径", 1024000);
-
if (!success)
-
Response.Write("下载文件出错!");
-
Page.Response.End();
ASP.NET文件下载函数代码为:
-
public static bool ResponseFile(HttpRequest _Request,HttpResponse _Response,string _fileName,string _fullPath, long _speed)
-
{
-
try
-
{
-
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
-
BinaryReader br = new BinaryReader(myFile);
-
try
-
{
-
_Response.AddHeader("Accept-Ranges", "bytes");
-
_Response.Buffer = false;
-
long fileLength = myFile.Length;
-
long startBytes = 0;
-
-
double pack = 10240;
-
-
int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;
-
if (_Request.Headers["Range"] != null)
-
{
-
_Response.StatusCode = 206;
-
string[] range = _Request.Headers["Range"].Split(new char[] {'=', '-'});
-
startBytes = Convert.ToInt64(range[1]);
-
}
-
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
-
if (startBytes != 0)
-
{
-
-
}
-
_Response.AddHeader("Connection", "Keep-Alive");
-
_Response.ContentType = "application/octet-stream";
-
_Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(_fileName,System.Text.Encoding.UTF8) );
-
-
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
-
int maxCount = (int) Math.Floor((fileLength - startBytes) / pack) + 1;
-
-
for (int i = 0; i < maxCount; i++)
-
{
-
if (_Response.IsClientConnected)
-
{
-
_Response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString())));
-
Thread.Sleep(sleep);
-
}
-
else
-
{
-
i=maxCount;
-
}
-
}
-
}
-
catch
-
{
-
return false;
-
}
-
finally
-
{
-
br.Close();
-
-
myFile.Close();
-
}
-
}
-
catch
-
{
-
return false;
-
}
-
return true;
-
}
这样就实现了文件下载时,不管是什么格式的文件,都能够弹出打开/保存窗口.
ASP.NET文件下载函数的基本情况就向你介绍到这里,希望对你了解ASP.NET文件下载函数有所帮助。
分享到:
相关推荐
ASP.NET文件下载函数.docASP.NET文件下载函数.doc
Asp.NET 文件上传下载 Asp.NET 文件上传下载 Asp.NET 文件上传下载 Asp.NET 文件上传下载
Asp.net文件下载Asp.net文件下载Asp.net文件下载Asp.net文件下载Asp.net文件下载Asp.net文件下载
asp.net文件下载demo 文件路径加密保护。 框架.net3.5
Asp.net回调函数源码,实现页面无刷新调用服务器端函数并返回结果,简单易懂,希望能给朋友们提高帮助!
描述:文件下载,内有使用方法
ASP.NET文件参考 asp.net asp.net技巧
ASP.NET C#下载任意类型文件的函数,可以实现任意类型的文件下载,使用方便
包含了asp.net的常用函数,数学函数,使用类型转换函数等等,绝对实用!
基于ASP.NET文件上传、下载的网络硬盘系统 源码
ASP.NET 中 Session 实现原理浅析 如 session的说明 使用等等
asp.net文件上传下载程序 有数据库 下载可用 很好的程序 精心制作
【ASP.NET编程知识】ASP.net处理XML数据实例浅析.docx
Asp.net文件在线管理系统源码_aspx开发教程.7z
asp.net文件管理 对文件的增加 删除 修改
B/S,ASP.NET文件下载源代码,文件下载,可支持多种文件格式,可以支接运行 B/S,ASP.NET文件下载源代码,文件下载,可支持多种文件格式,可以支接运行
Asp.Net函数集 函数 .net基础
asp.net c# 文件读写操作
asp.net C# 利用FTP 远程下载文件到指定盘符`
asp.net文件下载