0

For customer form I need to upload photo and store it on the file system, but after the whole form is populated and submitted.

I'm uploading file with HttpPostedFileBase using ajax file upload and I tried to store it in the session variable, and after the whole form is submitted to save it from session to file. Tried to convert it to byte array but with no luck.

Didn't find any sample for this to work.

Would someone please be kind to provide some samples how to convert HttpPostedFileBase fileUpload to byte array and back so that I can later store it in a file on server?

Chamila Maddumage
  • 3,304
  • 2
  • 32
  • 43
Rouda67
  • 9
  • 2
  • 1
    Hi, welcome to SO. You mention you have some code, if you could post what you have that would really help. https://stackoverflow.com/help/how-to-ask – indofraiser Mar 18 '19 at 12:32
  • Currently I created workaround with temp file (storing it at location TEMP, and after submit move it proper location). I tried something like in this post, but it doesn't work. https://stackoverflow.com/questions/26132792/session-storing-images-for-temporary-and-retrive-and-store-into-model – Rouda67 Mar 18 '19 at 12:45

1 Answers1

0

Found a solution for this problem; convert file to Base64, store it in session variable, and when form is submitted retrieve it from session:

//convert to Base64
System.IO.Stream fileStream = model.PersonPhotoFile.InputStream;
Byte[] fileToByte = new Byte[model.PersonPhotoFile.ContentLength];
model.PersonPhotoFile.InputStream.Position = 0;
model.PersonPhotoFile.InputStream.Read(fileToByte, 0, model.PersonPhotoFile.ContentLength);
string base64stringPhoto = Convert.ToBase64String(fileToByte);
fileStream.Close();

//convert from Base64 and write to file
Byte[] fileFromBytes = Convert.FromBase64String(base64stringPhoto);
System.IO.File.WriteAllBytes("D:\\Temp\\temp_from_base64.jpg", fileFromBytes);
Rouda67
  • 9
  • 2