【发布时间】:2015-09-27 13:55:36
【问题描述】:
我想将base64 编码为HttpPostedFileBase 接收的图像以将其发送到 json 对象中,但我不知道如何完成...请告诉我如何解码它回HttpPostedFileBase
【问题讨论】:
标签: c# json asp.net-mvc web-services http
我想将base64 编码为HttpPostedFileBase 接收的图像以将其发送到 json 对象中,但我不知道如何完成...请告诉我如何解码它回HttpPostedFileBase
【问题讨论】:
标签: c# json asp.net-mvc web-services http
我试过了,效果很好
string theFileName = Path.GetFileName(YourFile.FileName);
byte[] thePictureAsBytes = new byte[YourFile.ContentLength];
using (BinaryReader theReader = new BinaryReader(YourFile.InputStream))
{
thePictureAsBytes = theReader.ReadBytes(YourFile.ContentLength);
}
string thePictureDataAsString = Convert.ToBase64String(thePictureAsBytes);
【讨论】:
按照以下步骤将 HttpPostedFileBase 转换为 Base64String 类型
public ActionResult ParseCv(HttpPostedFileBase cvFile)
{
byte[] fileInBytes = new byte[cvFile.ContentLength];
using (BinaryReader theReader = new BinaryReader(cvFile.InputStream))
{
fileInBytes = theReader.ReadBytes(cvFile.ContentLength);
}
string fileAsString= Convert.ToBase64String(fileInBytes);
return Content(fileAsString);
}
【讨论】:
你可以这样做:
byte[] binaryData;
binaryData = new Byte[product.BrochureFile.InputStream.Length];
long bytesRead = product.BrochureFile.InputStream.Read(binaryData, 0, (int)product.BrochureFile.InputStream.Length);
product.BrochureFile.InputStream.Close();
string base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
【讨论】: