2016-10-17 60 views
0

我想存儲FileUpload對象,其中包含Session中的圖像。然後在下一頁上,我想將圖像保存到文件夾中。我能夠做到這一點沒有任何問題,但是當我嘗試存儲更大的圖像時,圖像變爲0kb,Windows照片瀏覽器顯示該文件爲空。這裏是我的代碼:ASP.NET - 使用會話存儲FileUpload對象

上傳窗體頁(C#):

//FileUploadPicture is of type FileUpload 
Session["fileupload_object"] = FileUploadPicture; 

圖片另存到文件夾頁(C#):

Boolean imageUploadStatus = false; 
string imageExtension = System.IO.Path. 
    GetExtension(((FileUpload)Session["fileupload_object"]).FileName); 
string[] acceptedImageExtensions = { ".gif", ".png", ".jpg", ".jpeg" }; 

for (int i = 0; i < acceptedImageExtensions.Length; i++) 
{ 
    if (imageExtension.Equals(acceptedImageExtensions[i], 
     StringComparison.InvariantCultureIgnoreCase)) 
     { 
      imageUploadStatus = true; 
     } 
} 

try 
{ 

((FileUpload)Session["fileupload_object"]).PostedFile. 
    SaveAs(imagePath + ((FileUpload)Session["fileupload_object"]).FileName); 

} 

catch (Exception ex) { } 

這裏是上傳的截圖圖片。

小圖像上傳就好了:

Small images uploaded without failure

稍大的圖像無法上傳:

744kB image turns 0kB after uploaded

所以,我怎樣才能解決這個問題呢?提前致謝。

更新代碼:

上傳窗體頁(C#):

Session["fileupload_filename"] = FileUploadPictureOfPaymentStatement.FileName; 
HttpPostedFile httpPostedFile = FileUploadPictureOfPaymentStatement.PostedFile; 
System.Drawing.Image image = Bitmap.FromStream(httpPostedFile.InputStream); 
Session["image"] = image; 

圖片另存到文件夾頁(C#):

Boolean imageUploadStatus = false; 
string imageExtension = System.IO.Path.GetExtension((Session["fileupload_filename"]) 
    .ToString()); 
string[] acceptedImageExtensions = { ".gif", ".png", ".jpg", ".jpeg" }; 
for (int i = 0; i < acceptedImageExtensions.Length; i++) 
{ 
    if (imageExtension.Equals(acceptedImageExtensions[i], 
     StringComparison.InvariantCultureIgnoreCase)) 
    { 
     imageUploadStatus = true; 
    } 
} 

if(imageUploadStatus) 
{ 
    try 
    { 
     ((System.Drawing.Image)Session["image"]) 
      .Save(imagePath + Session["fileupload_filename"].ToString()); 
    } catch(Exception ex) { } 
} 
+0

嗨,你有沒有試過[這](http://stackoverflow.com/questions/288612/how-to-increase-the-max-upload-file-size-in-asp-net)? –

+2

如果你確實真的需要在會話中存儲它,請不要存儲實際的fileUploadControl - 保存文件數據本身。 – Darren

+0

另外,你用'imageUploadStatus'做了什麼?你真的沒有驗證只保存被接受的擴展 –

回答

0

正如菲利普·Deguchi在評論中指出,這個問題只是一個重複類別爲this question

我只需要添加:

<system.web> 
<httpRuntime executionTimeout="90" 
      maxRequestLength="20000" 
      useFullyQualifiedRedirectUrl="false" 
      requestLengthDiskThreshold="8192"/> 
</system.web> 

Web.config