Asp.net mvc 4 如何在一个会话中进行并行处理?

Asp.net mvc 4 如何在一个会话中进行并行处理?,asp.net-mvc-4,Asp.net Mvc 4,我的下载功能如下。它在控制器类中: public void Download(string fileId) { // ************************************************** //MAKE FILEPATH string filePath = makeFilePath(fileId); string outFileName = _info.File

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

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();
                }
            }
        }
    }
一切正常,但当我下载文件时,在下载过程完成之前,我无法在控制器中执行其他操作。 我怎样才能解决这个问题


我认为问题在于服务器太忙,无法处理文件。服务器的响应门也很忙,因此,当客户端发送新请求时,它将挂起。

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

您能解释一下“无法在我的控制器中执行其他操作”是什么意思吗?另外,下载操作是如何调用的?通过Javascript?HTML链接?@DaveB84:我在浏览器中打开“新建”选项卡,粘贴其他操作的地址,然后输入。如果下载完成,将加载新页面。@DaveB84:下载将通过jquery ajax代码中的“window.location.href=…”调用