【问题标题】:Response TransmitFile响应传输文件
【发布时间】:2010-12-22 16:25:07
【问题描述】:

当用户单击下载链接时,我有以下代码将文件传递给用户。出于安全考虑,我不能直接链接到该文件,因此将其设置为解码 url 并传输文件。

一段时间以来一直运行良好,但最近我开始遇到文件开始下载的问题,但没有迹象表明文件有多大。

因此,当下载应该停止时,它不会。

文件大约 99mb,但是当我下载它时,浏览器一直在下载超过 100mb。我不知道它正在下载什么,但如果我不取消它,它就不会停止。

所以,我的问题是,是否有替代 TransmitFile 的方法或确保文件大小也被发送以便它在正确的时间停止的方法?

代码如下:

string filename = Path.GetFileName(url); 
context.Response.Buffer = true; 
context.Response.Charset = ""; 
context.Response.Cache.SetCacheability(HttpCacheability.NoCache); 
context.Response.ContentType = "application/x-rar-compressed"; 
context.Response.AddHeader("content-disposition", "attachment;filename=" + filename);   
context.Response.TransmitFile(context.Server.MapPath(url));
context.Response.Flush(); 

我不想使用WriteFile,因为我不想将整个文件加载到内存中,因为它太大了。

谢谢。

【问题讨论】:

  • 对不起,我忘了发布我的代码。字符串文件名 = Path.GetFileName(url); context.Response.Buffer = true; context.Response.Charset = ""; context.Response.Cache.SetCacheability(HttpCacheability.NoCache); context.Response.ContentType = "应用程序/x-rar-压缩"; context.Response.AddHeader("content-disposition", "attachment;filename=" + filename); context.Response.TransmitFile(context.Server.MapPath(url)); context.Response.Flush();
  • 我发现了一些我希望会有所帮助的代码,但它没有。 FileInfo OutFile = new FileInfo(context.Server.MapPath(url)); context.Response.Clear();字符串文件名 = Path.GetFileName(url); context.Response.Buffer = true; context.Response.Charset = ""; context.Response.Cache.SetCacheability(HttpCacheability.NoCache); context.Response.ContentType = "应用程序/x-rar-压缩"; context.Response.AddHeader("content-disposition","attachment;filename=" + filename); context.Response.TransmitFile(context.Server.MapPath(url), 0, OutFile.Length); context.Response.Flush();

标签: file size transmitfile


【解决方案1】:

我想我找到了答案。

我添加了以下代码,现在,至少对我而言,它报告了正确的文件大小并按预期工作。

FileInfo OutFile = new FileInfo(context.Server.MapPath(url));
long filesize = OutFile.Length; 
....
context.Response.AddHeader("Content-Length", filesize.ToString());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-31
    • 2015-07-21
    • 1970-01-01
    • 2011-01-25
    • 2017-02-25
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    相关资源
    最近更新 更多