base64 encode HttpPostedFileBase

送分小仙女□ 提交于 2020-02-20 09:12:10

问题


I want to base64 encode an image that is being received as HttpPostedFileBase to send it in a json object and I don't know how it can be done...and please tell me how can I decode it back to HttpPostedFileBase


回答1:


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



回答2:


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



回答3:


Follow the below steps to convert the HttpPostedFileBase to Base64String type

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


来源:https://stackoverflow.com/questions/31313280/base64-encode-httppostedfilebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!