C# Response.WriteFile vs Response.TransmitFile filesize issues

主宰稳场 提交于 2019-12-20 14:51:08

问题


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();
}

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/2110660/c-sharp-response-writefile-vs-response-transmitfile-filesize-issues

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!