如何在一个会话中并行执行?

时间:2015-06-30 07:32:07

标签: asp.net-mvc-4

我的下载功能如下。它在控制器类中:

public void Download(string fileId)
    {

            // **************************************************
            //MAKE FILEPATH
            string filePath = makeFilePath(fileId);
            string outFileName = _info.FileName;

            System.IO.Stream iStream = null;

            // Create buffer for reading [intBufferSize] bytes from file
            int intBufferSize = 10 * 1024;
            byte[] buffer = new System.Byte[intBufferSize];

            // Length of the file That Really Has Been Read From The Stream and Total bytes to read
            int length; long dataToRead;
            // **************************************************

            if (System.IO.File.Exists(filePath))
            {
                try
                {
                    // Open the file.
                    iStream = new System.IO.FileStream(
                        path: filePath,
                        mode: System.IO.FileMode.Open,
                        access: System.IO.FileAccess.Read,
                        share: System.IO.FileShare.Read);

                    // Total bytes to read:
                    dataToRead = iStream.Length;

                    // **************************************************
                    Response.Clear();
                    // Setting the unknown [ContentType]
                    // will display the saving dialog for the user
                    Response.ContentType = "application/octet-stream";
                    // With setting the file name,
                    // in the saving dialog, user will see
                    // the [outFileName] name instead of [download]!
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + outFileName);
                    // Notify user (client) the total file length
                    Response.AddHeader("Content-Length", iStream.Length.ToString());
                    // **************************************************

                    // Read the bytes.
                    while (dataToRead > 0)
                    {
                        // Verify that the client is connected.
                        if (Response.IsClientConnected)
                        {
                            // Read the data and put it in the buffer.
                            length = iStream.Read(buffer: buffer, offset: 0, count: intBufferSize);

                            // Write the data from buffer to the current output stream.
                            Response.OutputStream.Write(buffer: buffer, offset: 0, count: length);

                            // Flush (Send) the data to output
                            // (Don't buffer in server's RAM!)
                            Response.Flush();

                            buffer = new Byte[intBufferSize];
                            dataToRead = dataToRead - length;
                        }
                        else
                        {
                            //prevent infinite loop if user disconnects
                            dataToRead = -1;
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new ApplicationException(ex.Message);
                }
                finally
                {
                    if (iStream != null)
                    {
                        //Close the file.
                        iStream.Close();
                        iStream = null;
                    }
                    Response.Close();
                }
            }
        }
    }

一切正常,但是当我下载文件时,在下载过程完成之前,我无法在控制器中执行其他操作。 我该如何解决?

我认为问题是服务器太忙而无法处理文件。并且服务器的响应门也很忙,因此,当客户端发送新请求时,它将处于待处理状态。

1 个答案:

答案 0 :(得分:0)

我通过将属性[SessionState(SessionStateBehavior.ReadOnly)]放在控制器上解决了我的问题。 它运作良好。