较大PDF文件输出到客户端时出现白屏的解决方案

本文介绍了解决IIS处理大文件下载时CPU占用率高及内存使用量增加的问题。通过将大文件切割成小数据块逐步发送,以及调整Web.config配置来优化性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们的站点要提供 PDF 资源给用户下载,其中有一些 PDF 文件较大,运营一段时间出现 IIS 进程 CPU 占用率高,内存使用量增加,客户端的表现就是输出白屏。重启 IIS 后,才能正常对外提供服务。

原因:

当 ASP.NET 辅助进程(Aspnet_wp.exe,对于在 Internet 信息服务 6.0 [IIS] 上运行的应用程序,则为 W3wp.exe)执行文件下载请求时,向 Microsoft Internet 信息服务进程(Inetinfo.exe 或 Dllhost.exe)发送数据。

根据计算机的配置,IIS 进程可能会处理数据,也可能会将数据缓存在内存中。如果文件太大,在这两个进程相互通信的过程中,数据将被缓存在内存中。这可能会导致服务器上的内存使用量增加。出现此错误的原因是 Web 服务器上的内存限制

解决方法:

解决方法1

大文件切割成小数据块,然后逐步添加到输出流,MSDN上给出的代码样例

ContractedBlock.gif ExpandedBlockStart.gif Code
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->None.gifSystem.IO.StreamiStream=null;
None.gif
None.gif
//Buffertoread10Kbytesinchunk:
None.gif
byte[]buffer=newByte[10000];
None.gif
None.gif
//Lengthofthefile:
None.gif
intlength;
None.gif
None.gif
//Totalbytestoread:
None.gif
longdataToRead;
None.gif
None.gif
//Identifythefiletodownloadincludingitspath.
None.gif
stringfilepath="DownloadFileName";
None.gif
None.gif
//Identifythefilename.
None.gif
stringfilename=System.IO.Path.GetFileName(filepath);
None.gif
None.gif
try
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
//Openthefile.
InBlock.gif
iStream=newSystem.IO.FileStream(filepath,System.IO.FileMode.Open,
InBlock.gifSystem.IO.FileAccess.Read,System.IO.FileShare.Read);
InBlock.gif
InBlock.gif
InBlock.gif
//Totalbytestoread:
InBlock.gif
dataToRead=iStream.Length;
InBlock.gif
InBlock.gifResponse.ContentType
="application/octet-stream";
InBlock.gifResponse.AddHeader(
"Content-Disposition","attachment;filename="+filename);
InBlock.gif
InBlock.gif
//Readthebytes.
InBlock.gif
while(dataToRead>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//Verifythattheclientisconnected.
InBlock.gif
if(Response.IsClientConnected)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//Readthedatainbuffer.
InBlock.gif
length=iStream.Read(buffer,0,10000);
InBlock.gif
InBlock.gif
//Writethedatatothecurrentoutputstream.
InBlock.gif
Response.OutputStream.Write(buffer,0,length);
InBlock.gif
InBlock.gif
//FlushthedatatotheHTMLoutput.
InBlock.gif
Response.Flush();
InBlock.gif
InBlock.gifbuffer
=newByte[10000];
InBlock.gifdataToRead
=dataToRead-length;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//preventinfiniteloopifuserdisconnects
InBlock.gif
dataToRead=-1;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
catch(Exceptionex)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
//Traptheerror,ifany.
InBlock.gif
Response.Write("Error:"+ex.Message);
ExpandedBlockEnd.gif}

None.gif
finally
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
if(iStream!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//Closethefile.
InBlock.gif
iStream.Close();
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
解决方法 2

把站点的Web.config文件中的<compilation debug="true" batch="false">配置节修改为:<compilation debug="false" batch="false">

MSDN上的解释

当您在 ASP.NET 应用程序的 Web.config 文件中将编译元素的 debug 属性值设置为 false 时,必须针对要下载的文件大小将 Server.ScriptTimeout 属性设置为适当的值。默认情况下,Server.ScriptTimeout 值被设置为 90 秒。但是,当 debug 属性被设置为 true 时,Server.ScriptTimeout 值将被设置为一个非常大的值(30,000,000 秒)。作为一名开发人员,您必须知道这可能会对您的 ASP.NET Web 应用程序的行为造成的影响。

由于开发环境在我们建立Web应用的时候会默认将Web.config的这一配置节修改为可调试的状态,这将降低web应用程序的性能,所以我们在部署的时候常常会忽略掉这个配置。亡羊补牢,大家都检查一下自己的配置文件吧,Asp.net配置文件参考资料:

http://msdn.microsoft.com/zh-cn/architecture/kza1yk3a.aspx

坚强2002和你一起回头再说...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值