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
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
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With