较大PDF文件输出到客户端时出现白屏的解决方案
来源:互联网 发布:php防止csrf攻击 编辑:程序博客网 时间:2024/05/03 16:02
- Code
System.IO.Stream iStream = null;
// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000];
// Length of the file:
int length;
// Total bytes to read:
long dataToRead;
// Identify the file to download including its path.
string filepath = "DownloadFileName";
// Identify the file name.
string filename = System.IO.Path.GetFileName(filepath);
try
{
// Open the file.
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read,System.IO.FileShare.Read);
// Total bytes to read:
dataToRead = iStream.Length;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000);
// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);
// Flush the data to the HTML output.
Response.Flush();
buffer= new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
Response.Write("Error : " + ex.Message);
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
} - 解决方法2:
原因:
当 ASP.NET 辅助进程(Aspnet_wp.exe,对于在 Internet 信息服务 6.0 [IIS] 上运行的应用程序,则为 W3wp.exe)执行文件下载请求时,向 Microsoft Internet 信息服务进程(Inetinfo.exe 或 Dllhost.exe)发送数据。
根据计算机的配置,IIS 进程可能会处理数据,也可能会将数据缓存在内存中。如果文件太大,在这两个进程相互通信的过程中,数据将被缓存在内存中。这可能会导致服务器上的内存使用量增加。出现此错误的原因是 Web 服务器上的内存限制。
解决方法:
解决方法1:
大文件切割成小数据块,然后逐步添加到输出流,MSDN上给出的代码样例
把站点的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
- 较大PDF文件输出到客户端时出现白屏的解决方案
- 较大PDF文件输出到客户端时出现白屏的解决方案
- 较大PDF文件输出到客户端时出现白屏的解决方案
- 可以把较大的PDF文件拆分的工具
- 把一个较大的PDF文件分割用什么工具
- 怎么将较大的PDF文件进行分割
- 怎样把一篇较大的PDF文件转成PPT格式
- devexpress grid导出为pdf文件时出现 中文乱码的解决方案
- 可以把较大的PDF文件拆分成小PDF文件的方法
- Windows2003系统下,上传较大的文件时,出现“Request 对象 错误 'ASP 0104 : 80004005'”错误
- C# response输出要下载的文件流到客户端
- WORD另存为PDF时出现乱码的解决方案
- 数字输出到文件出现乱码的情况
- .net 输出文件到客户端并删除
- 读取文件并输出到客户端
- ireport输出到pdf
- ireport输出到pdf
- 输出datagridview到pdf
- HTML重构:工具篇
- HTML重构:战术篇
- HTML重构:战略篇
- 透过数据,横看成岭侧成峰
- 距离有多远?欧几里德距离&皮尔逊相关度系数
- 较大PDF文件输出到客户端时出现白屏的解决方案
- Session组件(增加和删除Session对象)
- 回头再说012 盘点我的2008年
- 《非诚勿扰》技术化解读
- 回头再说 011 金庸-读你千遍不厌倦
- WinHEC2008的惊喜
- 回头再说 010
- 回头再说 009
- 回头再说 008 瞬