我有一个 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();
            }

        }

最佳答案

将要

<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,但那是另一天。

关于asp.net - 大文件下载 - 与服务器重置连接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/187000/

10-16 20:26