照片图像上传和存储在会话中

问题描述 投票:0回答:1

对于客户表单,我需要上传照片并将其存储在文件系统上,但是在填写并提交整个表单之后。

我使用ajax文件上传使用HttpPostedFileBase上传文件,我尝试将其存储在会话变量中,并在提交整个表单后将其从会话保存到文件。试图将其转换为字节数组,但没有运气。

没有发现任何样品可供使用。

请有人提供一些示例如何将HttpPostedFileBase fileUpload转换为字节数组并返回以便以后将其存储在服务器上的文件中?

c# asp.net model-view-controller image-upload
1个回答
0
投票

找到了解决这个问题的方法;将文件转换为Base64,将其存储在会话变量中,并在提交表单时从会话中检索它:

//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);
© www.soinside.com 2019 - 2024. All rights reserved.