C# Response.WriteFile 与 Response.TransmitFile 文件大小问题
我在服务器上有一个 5Mb 的 pdf,使用 writeFile 下载此文件会得到 15Mb 的下载,而传输文件给出正确的 5Mb 文件大小...
这是由于 writeFile 在服务器上的内存中进行了某种解压缩吗?只是想知道是否有人见过同样的事情发生...
(ps 自从我们去了 iis7 后才注意到它??)
代码是...
if (File.Exists(filepath))
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("content-disposition","attachment;filename=\""+Path.GetFileName(filepath)+"\"");
HttpContext.Current.Response.AddHeader("content-length", new FileInfo(filepath).Length.ToString());
//HttpContext.Current.Response.WriteFile(filepath);
HttpContext.Current.Response.TransmitFile(filepath);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
}
I have a 5Mb pdf on the server dowloading this file using a writeFile gives me a 15Mb download, where as the transmitfile gives the correct 5Mb filesize...
Is this due to some sort of uncompression into memory on the server for the writeFile? Just wonder if anyone had seen the same thing happening...
(ps only noticed it since we went to iis7??)
code being...
if (File.Exists(filepath))
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("content-disposition","attachment;filename=\""+Path.GetFileName(filepath)+"\"");
HttpContext.Current.Response.AddHeader("content-length", new FileInfo(filepath).Length.ToString());
//HttpContext.Current.Response.WriteFile(filepath);
HttpContext.Current.Response.TransmitFile(filepath);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TransmitFile - 将指定文件直接写入 HTTP 响应输出流,而不将其缓冲在内存中。
WriteFile - 将指定文件直接写入 HTTP 响应输出流。
我想说出现这种差异是因为传输文件不缓冲它。写入文件使用缓冲(Afiak),基本上在传输数据之前临时保存数据,因此它无法猜测准确的文件大小,因为它是以块的形式写入的。
TransmitFile - Writes the specified file directly to an HTTP response output stream without buffering it in memory.
WriteFile - Writes the specified file directly to an HTTP response output stream.
I would say the difference occurs because Transmit file doesn't buffer it. Write file is using buffering (Afiak), basically temporarily holding the data before transmitting it, as such it cannot guess the accurate file size because its writing it in chunks.
你可以通过下面的定义来理解。
Response.TransmitFile VS Response.WriteFile:
TransmitFile:此方法将文件发送到客户端,而不将其加载到服务器上的应用程序内存中。如果下载的文件很大,这是理想的使用方法。
WriteFile:此方法将正在下载的文件加载到服务器内存中,然后再将其发送到客户端。如果文件很大,则 ASPNET 工作进程可能会重新启动。*
参考:- TransmitFile VS WriteFile
You can understand by following definition.
Response.TransmitFile VS Response.WriteFile:
TransmitFile: This method sends the file to the client without loading it to the Application memory on the server. It is the ideal way to use it if the file size being download is large.
WriteFile: This method loads the file being download to the server's memory before sending it to the client. If the file size is large, you might the ASPNET worker process might get restarted.*
Reference :- TransmitFile VS WriteFile