【问题标题】:Large File Download - Connection With Server Reset大文件下载 - 连接服务器重置
【发布时间】:2008-10-09 12:18:42
【问题描述】:

我有一个 asp.net 网站,允许用户下载较大的文件 - 30mb 到大约 60mb。有时下载工作正常,但通常在下载完成之前的某个不同时间点失败,并显示与服务器的连接已重置的消息。

最初我只是使用 Server.TransmitFile,但在阅读了一些内容后,我现在正在使用下面发布的代码。我还在 Page_Init 事件中将 Server.ScriptTimeout 值设置为 3600。

private void DownloadFile(string fname, bool forceDownload)
        {
            string path = MapPath(fname);
            string name = Path.GetFileName(path);
            string ext = Path.GetExtension(path);
            string type = "";

            // set known types based on file extension  

            if (ext != null)
            {
                switch (ext.ToLower())
                {
                    case ".mp3":
                        type = "audio/mpeg";
                        break;

                    case ".htm":
                    case ".html":
                        type = "text/HTML";
                        break;

                    case ".txt":
                        type = "text/plain";
                        break;

                    case ".doc":
                    case ".rtf":
                        type = "Application/msword";
                        break;
                }
            }

            if (forceDownload)
            {
                Response.AppendHeader("content-disposition",
                    "attachment; filename=" + name.Replace(" ", "_"));
            }

            if (type != "")
            {
                Response.ContentType = type;
            }
            else
            {
                Response.ContentType = "application/x-msdownload";
            }

            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;

            try
            {
                // Open the file.
                iStream = new System.IO.FileStream(path, 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();
                }
                Response.Close();
            }

        }

【问题讨论】:

    标签: asp.net download


    【解决方案1】:

    <configuration>
      <system.web>
        <httpRuntime executionTimeout="3600"/>
      </system.web>
    </configuration>
    

    有什么帮助吗?

    写入数据的内部循环似乎有点复杂,我至少将其更改为:

    int length;
    while(  Response.IsClientConnected && 
           (length=iStream.Read(buffer,0,buffer.Length))>0 ) 
    {
      Response.OutputStream.Write(buffer,0,length);
      Response.Flush();
    }
    

    无需在循环中每轮重新分配缓冲区,您可以在将其写入输出后简单地重新使用它。

    进一步的改进是使用异步 IO,但那是另一天。

    【讨论】:

      【解决方案2】:

      我在使用 FileUpload Control 并且上传文件大小 >4MB 时遇到了类似的问题。我曾经收到“连接已重置”错误页面。这是我解决问题所遵循的步骤:

      转到 web.config 文件并设置适合您希望上传的文件类型的大小限制。默认大小限制为 4096 千字节 (KB) 或 4 兆字节 (MB)。您可以通过设置 httpRuntime 元素的 maxRequestLength 属性来允许上传较大的文件。要增加整个应用程序的最大允许文件大小,请在 Web.config 文件中设置 maxRequestLength 属性。

      例如,允许 10MB (10240 KB) 文件..我使用(用 '' 替换 ']')

      [配置]
      [system.web]
      [httpRuntime maxRequestLength="10240"/]
      [/system.web]
      [/配置]

      【讨论】:

        【解决方案3】:

        此问题的最终修复是在 web.config 文件中进行修改。我只需将 sessionState mode="InProc" 更改为 sessionState mode="StateServer"。

        【讨论】:

        • 为什么?你能解释一下原因吗?
        【解决方案4】:

        最终对我有用的是执行 Response.End 并使用文件流执行 using 语句。这是我的代码:

        public partial class ssl_Report_StreamReport : BaseReportPage
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                //Get the parameters
                string reportName = Utils.ParseStringRequest(Request, "reportName") ?? string.Empty;
                string reportGuid = Session["reportGuid"].ToString();
                string path = Path.Combine(ReportPath(), Utils.GetSessionReportName(reportName, reportGuid));
        
                using (var fileStream = File.Open(path, FileMode.Open))
                {
                    Response.ClearHeaders();
                    Response.Clear();
                    Response.ContentType = "application/octet-stream";
                    Response.AddHeader("Content-Disposition", "attachment; filename=\"" + reportName + "\"");
                    Response.AddHeader("Content-Length", fileStream.Length.ToString(CultureInfo.InvariantCulture));
                    StreamHelper.CopyStream(fileStream, Response.OutputStream);
                    Response.Flush();
                    Response.End();
                }
        
                ReportProcessor.ClearReport(Session.SessionID, path);
            }
        }
        
        
        public static class StreamHelper
        {
            public static void CopyStream(Stream input, Stream output)
            {
                byte[] buffer = new byte[32768];
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    output.Write(buffer, 0, read);
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2012-07-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-16
          • 1970-01-01
          相关资源
          最近更新 更多