title: ASP.NET - Using Session to store FileUpload object
body: <p>I would like to store a <code>FileUpload</code> object which contains an image in a <code>Session</code>. 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:</p>
  
  <p><strong>Upload Form Page (C#):</strong></p>
  
  <pre><code>//FileUploadPicture is of type FileUpload
  Session["fileupload_object"] = FileUploadPicture;
  </code></pre>
  
  <p><strong>Save Picture to Folder Page (C#):</strong></p>
  
  <pre><code>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 &lt; 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) { }
  </code></pre>
  
  <p>And here are the screenshots of the uploaded images.</p>
  
  <p><strong>Small image uploaded just fine:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/rvjq2.jpg" rel="nofollow">Small images uploaded without failure</a></p>
  
  <p><strong>Slightly larger image fails to be uploaded:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/2FGVH.jpg" rel="nofollow">744kB image turns 0kB after uploaded</a></p>
  
  <p>So, how can I remedy this problem ? Thanks in advance.</p>
  
  <p><strong>Updated code:</strong></p>
  
  <p><strong>Upload Form Page (C#):</strong></p>
  
  <pre><code>Session["fileupload_filename"] = FileUploadPictureOfPaymentStatement.FileName;
  HttpPostedFile httpPostedFile = FileUploadPictureOfPaymentStatement.PostedFile;
  System.Drawing.Image image = Bitmap.FromStream(httpPostedFile.InputStream);
  Session["image"] = image;
  </code></pre>
  
  <p><strong>Save Picture to Folder Page (C#):</strong></p>
  
  <pre><code>Boolean imageUploadStatus = false;
  string imageExtension = System.IO.Path.GetExtension((Session["fileupload_filename"])
      .ToString());
  string[] acceptedImageExtensions = { ".gif", ".png", ".jpg", ".jpeg" };
  for (int i = 0; i &lt; 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) { }
  }
  </code></pre>