C#Response.WriteFile VS Response.TransmitFile文件大小问题 [英] C# Response.WriteFile vs Response.TransmitFile filesize issues

查看:1242
本文介绍了C#Response.WriteFile VS Response.TransmitFile文件大小问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有服务器使用WriteFile的你下载这个文件的一个5MB的PDF给我一个下载15兆字节,其中作为的TransmitFile给出了正确的文件大小为5Mb ...



这是由于某种解压缩到内存在服务器上为WriteFile的呢?只是不知道是否有人看到了发生的事情...



同样的事情

(PS只注意到它,因为我们去IIS7?)



代码是...

 如果(File.Exists(文件路径))
$ { b $ b HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType =应用程序/八位字节流;
HttpContext.Current.Response.AddHeader(内容处置,附件;文件名= \+ Path.GetFileName(文件路径)+\);
HttpContext.Current.Response.AddHeader(内容长度,新的FileInfo(文件路径).Length.ToString());

//HttpContext.Current.Response.WriteFile(filepath);
HttpContext.Current.Response.TransmitFile(文件路径);

HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
}


解决方案

的TransmitFile - 写入指定的文件直接将无存储器缓冲它HTTP响应输出流。



WriteFile的 - 直接写入指定的文件到一个HTTP响应输出流。



我想说的差异发生。因为发送的文件不缓冲它。写文件是使用缓冲(Afiak),基本上传输前暂时保持数据,因此它无法猜测准确的文件大小,因为它写它块。


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

解决方案

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.

这篇关于C#Response.WriteFile VS Response.TransmitFile文件大小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆