在asp.net开始提供文件下载时,w3wp(2003中的)就已经开始向iis写文件内容了,不会等待客户端点击开始下载的按钮,如果文件过于庞大,会占用很多服务器的内存,这时可能出现下载失败的情况。
解决的方法是每次由asp.net向输出流写一小块数据,循环,直到写完为止。
需要注意的是web.config里面的compilation配置块中的debug属性不可以在生产服务器上配置为true,因为这样会导致脚本执行超时时间被自动配置成很大的值:30,000,000 秒
解决的方法是每次由asp.net向输出流写一小块数据,循环,直到写完为止。
需要注意的是web.config里面的compilation配置块中的debug属性不可以在生产服务器上配置为true,因为这样会导致脚本执行超时时间被自动配置成很大的值:30,000,000 秒
代码例子: // 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; } }