简体   繁体   English

base64 编码 HttpPostedFileBase

[英]base64 encode HttpPostedFileBase

我想对作为HttpPostedFileBase接收的图像进行base64编码以将其发送到 json 对象中,但我不知道如何完成...请告诉我如何将其解码回HttpPostedFileBase

I tried this and it worked我试过了,它奏效了

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);

Follow the below steps to convert the HttpPostedFileBase to Base64String type按照以下步骤将 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);
    }

You can do this:你可以这样做:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM