2016-11-16 128 views
0

我知道還有其他幾個像這樣的問題,但是我從很多網站,SO,MSDN等都已經嘗試過的所有答案都已經證明對錯誤沒有影響。ASP.NET FileUpload無法訪問已關閉的文件

我有一個asp:FileUpload,其中,在更改時,我存儲在一個會話變量(我有其他領域,導致回發的,所以需要確保我有文件的用戶上傳的地方)。

我得到的錯誤是

{"Cannot access a closed file."} 
Data: {System.Collections.ListDictionaryInternal} 
HResult: -2146232798 
HelpLink: null 
InnerException: null 
Message: "Cannot access a closed file." 
ObjectName: "" 
Source: "mscorlib" 
StackTrace: " at System.IO.__Error.FileNotOpen()\r\n at System.IO.FileStream.Seek(Int64 offset, SeekOrigin origin)\r\n at System.Web.HttpRawUploadedContent.TempFile.GetBytes(Int32 offset, Int32 length, Byte[] buffer, Int32 bufferOffset)\r\n at System.Web.HttpRawUploadedContent.CopyBytes(Int32 offset, Byte[] buffer, Int32 bufferOffset, Int32 length)\r\n at System.Web.HttpInputStream.Read(Byte[] buffer, Int32 offset, Int32 count)\r\n at GCSearchPortal.GlobalFunctions.UploadFile[T](HttpServerUtility server, FileType type, T file, Int32 id) in C:\\Users\\zach.ross-clyne\\Source\\Workspaces\\GC Search Portal\\GCSearchPortal\\GCSearchPortal\\GlobalFunctions.cs:line 241" 
TargetSite: {Void FileNotOpen()} 

這是什麼在我的web.config中system.web部分

<httpRuntime useFullyQualifiedRedirectUrl="true" maxRequestLength="1048576" requestLengthDiskThreshold="1073741824"/> 

,導致該問題的代碼是這樣的:

internal static int UploadFile<T>(HttpServerUtility server, FileType type, T file, int id) 
{ 
    if (typeof(T) == typeof(HtmlInputFile) || typeof(T) == typeof(FileUpload)) 
    { 
     string fileName = null; 
     string location = null; 

     dynamic fileUpload = file; 

     if (fileUpload.PostedFile != null && fileUpload.PostedFile.ContentLength > 0) 
     { 
      try 
      { 
       /* Get a reference to PostedFile object */ 
       HttpPostedFile attFile = fileUpload.PostedFile; 

       /* Get size of the file */ 
       int attachFileLength = attFile.ContentLength; 
       /* Make sure the size of the file is > 0 */ 
       if (attachFileLength > 0) 
       { 
        if (attachFileLength < 52428800) 
        { 
         /* Get extension for file */ 
         string ext = Path.GetExtension(attFile.FileName); 

         switch (type) 
         { 
          case FileType.TermsOfBusiness: 
           location = "~/Clients/" + id + "/"; 
           fileName = "TOB"; 
           break; 
          case FileType.TermsOfRole: 
           location = "~/Jobs/" + id + "/"; 
           fileName = "TOR"; 
           break; 
          case FileType.CandidateCV: 
           location = "~/Candidates/" + id + "/"; 
           fileName = "CV"; 
           break; 
          case FileType.NewCV: 
           location = "~/CVs/"; 
           fileName = id.ToString(); 
           break; 
         } 

         fileName += ext; 

         /* Save the file on the server */ 
         System.IO.Directory.CreateDirectory(server.MapPath(location)); 

         string filePath = attFile.FileName; 
         string fileName2 = fileUpload.FileName; 
         Stream fStream = fileUpload.FileContent; 
         byte[] contents = new byte[fStream.Length]; 

         fStream.Read(contents, 0, (int)fStream.Length); // <<<<< This line causes the crash 

         File.WriteAllBytes(server.MapPath(location + fileName), contents); 

         // This is what the code was at the very beginning before all my changes 
         //attFile.SaveAs(server.MapPath(location + fileName)); 

         return 2; 
        } 
        else 
        { 
         return 3; 
        } 
       } 
       else 
       { 
        return 1; 
       } 
      } 
      catch (Exception ex) 
      { 
       return 1; // <<<<< This is where it's crashing and giving the error above 
      } 
     } 
     else 
     { 
      return 0; 
     } 
    } 
    else 
    { 
     throw new Exception("Passed type is not supported"); 
    } 
} 

感謝您的任何幫助提前

+1

什麼行實際上拋出異常?不是實際的catch語句,而是引起catch的行。 – Hank

+0

'fStream.Read'而這在使用時的註釋'attFile.SaveAs'(這是初始代碼) –

+0

變化useFullyQualifiedRedirectUrl爲假, <的httpRuntime executionTimeout = 「90」 的maxRequestLength = 「20000」 useFullyQualifiedRedirectUrl =「假「requestLengthDiskThreshold =」8192「/>

回答

0

試試這個:

int fileLength = attFile.ContentLength; 

byte[] byteContent = new byte[fileLength]; 
attFile.InputStream.Read(byteContent, 0, iLength); 
using (var memStream = new MemoryStream(byteContent)) 
{ 
    System.IO.File.WriteAllBytes(server.MapPath(location + fileName), memStream .ToArray()); 
} 
+0

仍然有完全相同的問題。它崩潰的行是'attFile.InputStream.CopyTo'。 –

+0

@ ZachRoss-Clyne你是否檢查過InputStream屬性它是否有任何內容? – CSL

+0

它有433994的長度,所以我想象它呢? –

相關問題