本人想批量上传照片,在local的时候,一切功能都正常,上传文件存数据库以及存相片文件夹都正常。
可是当把程序放到server上,上传一张照片就会报错,当上传四张以上,第一张正常,第二张以后就抱错,抱错的代码为:file.SaveAs(Server.MapPath(relativepath));察看log里面的纪录说是:System.ObjectDisposedException: Cannot access a closed file.
不知道为什么了,恳请大家帮忙!谢谢!

解决方案 »

  1.   

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Session["HttpPostedFileSession"] != null)
            {
                List<HttpPostedFile> postedFileList = (List<HttpPostedFile>)Session["HttpPostedFileSession"];            foreach (HttpPostedFile f in postedFileList)
                {
                    //f.SaveAs(HttpContext.Current.Request.PhysicalApplicationPath + "FileModule/MultiUploader/upload/" + f.FileName);                HttpFileCollection uploadFile = Request.Files;
                    string fileFormat, fileName, GUIDName, GUIDFileName;
                    int fileTypeID = int.Parse(ddlFileTypeDropDown.selectedValue);
                    string extensionType = FileModule.FileTypeTable.getExtensionByFileTypeID(fileTypeID);            if (!string.IsNullOrEmpty(f.FileName))
                {
                    string[] file = f.FileName.Split('.');
                    fileFormat = file[1];
                    fileName = file[0];                GUIDFileName = System.Guid.NewGuid().ToString();
                    GUIDName = GUIDFileName + "." + fileFormat;
    if (extensionType.Contains(fileFormat))
                        {
                            try
                            {
                                string memberZone = FileModule.FileModuleType.getModulePath(moduleType) + moduleID + Path.AltDirectorySeparatorChar + FileModule.FileTypeTable.getTypeName(ddlFileTypeDropDown.selectedValue) + Path.AltDirectorySeparatorChar;
                                string relativepath = memberZone + GUIDName;
                                f.SaveAs(Server.MapPath(relativepath));                            if (File.Exists(Server.MapPath(relativepath)))
                                {                                if (saveToDB(f.FileName, relativepath))
                                    {
                                        lblmsg.Text ="Upload Successfully @" + DateTime.Now;
                                        if (this.UpdateResultEvent != null)
                                        {
                                            this.UpdateResultEvent(true);
                                        }                                }
                                    else
                                    {
                                        File.Delete(Server.MapPath(relativepath));
                                        lblmsg.Text = "Insert Fail";
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Common.writelog(ex.ToString());
                                lblmsg.Text = "Fail Upload";
                            }
                        }
                        else
                        {
                            lblmsg.Text = "Incorrect File Type!";
                        }
                    }                else
                    {
                        lblmsg.Text = "Please Upload a File";
                    }            }
            
            Session.Clear();
            }
        }
      

  2.   

    是不是文件超过限制呢!看看webconfig 的httpRuntime节