base64编码HttpPostedFileBase

时间:2015-07-09 09:22:04

标签: c# json asp.net-mvc web-services http

我希望base64将正在接收的图片编码为HttpPostedFileBase,以便将其发送到json对象中,我不知道如何完成...并且请告知我如何将其解码回HttpPostedFileBase

3 个答案:

答案 0 :(得分:11)

我试过这个并且有效

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

答案 1 :(得分:0)

你可以这样做:

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

答案 2 :(得分:0)

按照以下步骤将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);
    }