Response.TransmitFile 发送零字节文件

问题描述 投票:0回答:3

我正在尝试通过自定义处理程序下载 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
3个回答
0
投票

也许你需要

Content-Length

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

或者也许你错过了

ContentType

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

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

context.Response.Cache.SetNoStore();

0
投票

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


0
投票

代码的哪一部分?我也有同样的问题

© www.soinside.com 2019 - 2024. All rights reserved.