I would like to store a FileUpload
object which contains an image in a Session
. Then on the next page, I would like to save the image into a folder. I am able to do that without any problem but when I try to store a larger image, the image becomes 0kb and Windows Photo Viewer says that the file is empty. Here's my code:
Upload Form Page (C#):
//FileUploadPicture is of type FileUpload
Session["fileupload_object"] = FileUploadPicture;
Save Picture to Folder Page (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) { }
And here are the screenshots of the uploaded images.
Small image uploaded just fine:
Small images uploaded without failure
Slightly larger image fails to be uploaded:
744kB image turns 0kB after uploaded
So, how can I remedy this problem ? Thanks in advance.
Updated code:
Upload Form Page (C#):
Session["fileupload_filename"] = FileUploadPictureOfPaymentStatement.FileName;
HttpPostedFile httpPostedFile = FileUploadPictureOfPaymentStatement.PostedFile;
System.Drawing.Image image = Bitmap.FromStream(httpPostedFile.InputStream);
Session["image"] = image;
Save Picture to Folder Page (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) { }
}