【问题标题】:Response.TransmitFile sending zero byte fileResponse.TransmitFile 发送零字节文件
【发布时间】:2011-02-21 03:58:32
【问题描述】:

我正在尝试通过自定义处理程序下载一个 zip 文件。该文件作为零字节 zip 文件下载。但原始文件不是零字节 zip 文件。 代码是ProcessRequest(HttpContext context)是

            String file = Directory.GetFiles(cachePath).FirstOrDefault();
            String filename = Path.GetFileName(file);

            context.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
            context.Response.ContentType = "application/zip";
            //byte[] bytes = File.ReadAllBytes(file);
            //context.Response.BinaryWrite(bytes);
            context.Response.TransmitFile(file);
            context.Response.Flush();

【问题讨论】:

    标签: c# asp.net download httphandler


    【解决方案1】:

    也许你需要Content-Length

    context.Response.AddHeader("Content-Length", new FileInfo(file).Length.ToString());
    

    或者你可能错过了ContentType

    context.Response.ContentType = "application/octet-stream";
    

    作为最后的手段,它可能与缓存有关。

    context.Response.Cache.SetNoStore();
    

    【讨论】:

    • 感谢您的帮助。我没有发布完整的代码,实际问题出在代码的其他部分。
    【解决方案2】:

    该代码看起来很准确。我正在使用几乎相同的 sn-p 在我的网站上执行文件下载。我唯一看不到的是您从中检索文件的路径。您可能需要执行 HostingEnvironment.ApplicationPhysicalPath 来获取文件的实际路径。

    【讨论】:

    • 谢谢。我有问题是我没有发布的代码的其他部分
    猜你喜欢
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 2011-04-13
    • 1970-01-01
    相关资源
    最近更新 更多