Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

base64 encode HttpPostedFileBase

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

like image 960
Abdallah Avatar asked Jul 09 '15 09:07

Abdallah


2 Answers

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);
like image 65
Abdallah Avatar answered Oct 03 '22 05:10

Abdallah


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);
    }
like image 26
Pradeep Kumar Das Avatar answered Oct 03 '22 03:10

Pradeep Kumar Das