2

I am trying to convert an image to base64 to upload it on sharepoint site but it is throwing 400:bad request error. when i checked properly then i found out that the base64 i am sending is endcoded by javascript and it is different than what is expected by sharepoint. I have attached 2 images here describing the difference. Can anyone help me to get the proper encoded data using javascript ?

javascript encoded base64

c# encoded base64

var files = $("#myfile").get(0).files;

        var reader = new FileReader();
        reader.readAsDataURL(files[0]);

        reader.onload = function () {
          console.log(reader.result);
        }
Darpan Pathak
  • 86
  • 1
  • 6
  • Refer this: http://stackoverflow.com/questions/21325661/convert-image-path-to-base64-string – Dil85 Nov 22 '16 at 12:58

2 Answers2

0

Could try : reader.result.split("base64,")[1]

Removes the "base64," start of the string.

mvc_help
  • 187
  • 9
0
    Please try this , i am using this in my project , its working for me



     if (file.ContentType.Contains("image"))
                {
                    string theFileName = Path.GetFileName(file.FileName);
                    byte[] thePictureAsBytes = new byte[file.ContentLength];
                    using (BinaryReader theReader = new BinaryReader(file.InputStream))
                    {
                        thePictureAsBytes = theReader.ReadBytes(file.ContentLength);
                    }
                    string thePictureDataAsString = Convert.ToBase64String(thePictureAsBytes);

                }


    "thePictureDataAsString " variable got Base64 string 


.........................................................................


i am getting file like this in my project

 public ActionResult SaveMake(string inputMakeName, HttpPostedFileBase file)
        {
            MakeModel objMakeModel = new MakeModel();
            if (file.ContentType.Contains("image"))
            {
                string theFileName = Path.GetFileName(file.FileName);
                byte[] thePictureAsBytes = new byte[file.ContentLength];
                using (BinaryReader theReader = new BinaryReader(file.InputStream))
                {
                    thePictureAsBytes = theReader.ReadBytes(file.ContentLength);
                }
                string thePictureDataAsString = Convert.ToBase64String(thePictureAsBytes);
                objMakeModel.ImageBase64 = thePictureDataAsString;
                objMakeModel.Make1 = inputMakeName;
            }

            string response = _apiHelper.ConvertIntoReturnStringPostRequest<MakeModel>(objMakeModel, "api/Transaction/SaveMakes/");
          //  string response = _apiHelper.SaveMake(objMakeModel, "api/Transaction/SaveMakes/");
            return RedirectToAction("AddVehicleMaintenance");
        }
AmanMiddha
  • 31
  • 1
  • 9