【问题标题】:Photo image upload and store in session照片图像上传并存储在会话中
【发布时间】:2019-08-08 19:40:06
【问题描述】:

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

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

没有找到任何可用的示例。

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

【问题讨论】:

  • 嗨,欢迎来到 SO。你提到你有一些代码,如果你能发布你拥有的东西,那真的很有帮助。 stackoverflow.com/help/how-to-ask
  • 目前我使用临时文件创建了解决方法(将其存储在位置 TEMP,并在提交后将其移动到正确位置)。我在这篇文章中尝试过类似的方法,但它不起作用。 stackoverflow.com/questions/26132792/…

标签: c# asp.net model-view-controller image-upload


【解决方案1】:

找到了解决这个问题的办法;将文件转换为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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    • 2014-11-25
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多