当前位置: 代码迷 >> ASP.NET >> MVC Controller处理文件下传,ashx获取下传信息。必须等Controller存储完后才能读取。求解
  详细解决方案

MVC Controller处理文件下传,ashx获取下传信息。必须等Controller存储完后才能读取。求解

热度:7368   发布时间:2013-02-25 00:00:00.0
MVC Controller处理文件上传,ashx获取上传信息。必须等Controller存储完后才能读取。。求解
我在一个MVC的controller中处理文件上传,其中进度信息保存在session中,然后通过AJAX请求一个ashx来读取session进度。但是AJAX的请求发到服务端后,必须要等controller的文件保存完毕才能读取session的值。此时session里记录的文件进度已经是100%了。AJAX返回的数据一下全部都是100%,没能达到监听进度的木的,求达人详解。下面是相关代码

 controller处理文件部分

            FileProgress progress = new FileProgress(); 
            int length = ulf.FileLength;
            progress.TotalSize = length;
            byte[] FileArray = new Byte[length]; 
            int index = 0; 
            byte[] b = new byte[5000];
            int getByteSize = ulf.FileData.Read(b, 0, b.Length);
            while (getByteSize > 0)
            {
                Array.Copy(b, 0, FileArray, index, getByteSize);
                index += getByteSize;
                Thread.Sleep(100);
                progress.CurrSize = index;
                HttpContext.Current.Session[file_key] = progress; 
                getByteSize = ulf.FileData.Read(b, 0, b.Length);
            }

            FileAttachment attach = new FileAttachment();
            attach.FieldName = ulf.ConditionField;
            attach.FileData = FileArray;
            attach.FileLength = length;
            attach.FileName = ulf.FileName;
            attach.FormId = new Guid(ulf.FormId);
            attach.InstanceId = new Guid(ulf.InstanceId);

ashx读取session部分

public class FileUploadProgressHandler : IHttpHandler,IRequiresSessionState 
    {
        Double Percentage = 0.00;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";

            FileProgress progress = (FileProgress)context.Session[context.Request["sessionId"]];//request. .Cache[guid] as DownloadingFileInfo;
  相关解决方案