none
Thread调用函数上传文件报‘无法访问已关闭的文件’(On a thread to upload file problem) RRS feed

  • 问题

  • 该线程在上传文件(用FileUpload转换成流上传)前先操作了两次数据库,然后才去执行上传文件,此时如果上传的文件大于8M就会发生‘无法访问已关闭的文件’的错误。进过排除,并没有达到程序对上传文件的大小限制,而且如果把操作数据库的代码注释掉的话是可以正常上传大于8M文件的。我上传文件是单个上传的,并且如果不放在线程去上传的话,也是可以成功上传大于8M的文件。在此请教各位高手解释下这是什么原因,不胜感激。

    FOR ENGLISH:The thread in the upload file ( FileUpload into flow upload ) before operation two database, and then to perform the upload file, if the file is larger than 8M will happen ' cannot access a closed file ' error. After elimination, and did not reach the program to upload file size limit, and if the database operation code comment out you can upload files larger than normal 8M. I uploaded file is a single upload, and if not on the thread to upload it, can be successfully uploaded files larger than 8M. Ask you master this explain why this is, be grateful.
      int UploadedLength = 0;
            int bufferSize = 1000;
            byte[] buffer = new byte[bufferSize];
            
            try
            {
    
                using (FileStream fs = new FileStream(Path.Combine(path, strpath), FileMode.Create))
                {
                    
                    while (UploadedLength < ContentLength)
                    {
                        int bytes = this.FileUpload1.PostedFile.InputStream.Read(buffer, 0, bufferSize);
                        fs.Write(buffer, 0, bytes);
                        UploadedLength += bytes;
                    }
                    Page.ClientScript.RegisterStartupScript(typeof(string), string.Empty, "alert('上传完毕!')", true);
                    fs.Close();
                }
    
            }
            catch (Exception ex)
            {
    
                Session["PlanMsg"] = "文件上传失败!(" + ex.Message + ")|0";
                SysOpMsg.SetErrMsg("文件上传失败!(" + ex.Message + ")");
               
               
            }


    2012年12月19日 3:28

全部回复