首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TransmitFile缺少文件末尾的单个字节

TransmitFile缺少文件末尾的单个字节
EN

Stack Overflow用户
提问于 2012-07-04 21:28:10
回答 1查看 1.2K关注 0票数 3

传入的参数有:

代码语言:javascript
复制
`C:\Licenses\testfolder\PERSONAL-Wednesday 04 July-0405.txt`,`c2license.txt`

它的功能是:

代码语言:javascript
复制
/// <summary>
/// Starts serving the download
/// </summary>
public static void InitStoreDownload(string filePath, string serveFileName)
{
    // Get size of file
    var f = new FileInfo(filePath);

    var fileSize = f.Length;
    var extension = f.Extension;

    var context = HttpContext.Current;

    context.Response.Clear();
    context.Response.Buffer = false;

    // Correct mime type
    if (extension.Equals(".zip", StringComparison.CurrentCultureIgnoreCase))
        context.Response.ContentType = "application/octet-stream";
    else if (extension.Equals(".txt", StringComparison.CurrentCultureIgnoreCase))
    {
        context.Response.ContentType = "text/plain";
    }

    context.Response.AddHeader("Content-Disposition", "attachment; filename=" + serveFileName);
    context.Response.AddHeader("Content-Length", fileSize.ToString());
    context.Response.TransmitFile(filePath);
    context.Response.Close();

    context.Response.End();
}

服务器上的C:\Licenses\testfolder\PERSONAL-Wednesday 04 July-0405.txt文件的长度为475字节。

使用此脚本获取时下载的文件为474字节,文件末尾缺少一个字节。(最后一个字节是句号,存在于服务器上的文件中,但通过此函数下载时不存在)。这会导致文件变得无效。

我们正在挠头,试图找出为什么一个字节丢失了,有人能帮上忙吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-04 21:33:25

试着使用

代码语言:javascript
复制
Response.TransmitFile(filePath);
HttpContext.Current.ApplicationInstance.CompleteRequest(); 

而不是

代码语言:javascript
复制
Response.Close();
Response.End();

或者像其他人提到的那样:

Close()之前调用Flush()

代码语言:javascript
复制
Response.TransmitFile(filePath);
Response.Flush();
Response.Close();
Response.End();

或者省略对Close()的调用,直接调用End(),导致包含刷新响应。

代码语言:javascript
复制
Response.TransmitFile(filePath);
Response.End();

这里有一个关于Response.End()的thread,也许它包含了对你有用的信息。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11330076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档