0

I want to select multiple images and convert them to Base64 strings. I pushed the files into one array. My requirement is that after converting into Base64 strings, I want to push them into an array. I am not able to convert the images to Base64 strings.

$("input[name=property_images]").change(function() {
    var names = [];
    for (var i = 0; i < $(this).get(0).files.length; ++i) {
        names.push($(this).get(0).files[i].name);
    }
	console.log(names);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<input type="file" name="property_images" multiple="multiple" />

1
  • I've done this with Angular before, and just warning you that if you have a image bigger than an icon than your browser experiences major problems handling a string as long as a base64. It crashed my chrome and I couldn't use the base64 method at all! Commented Jan 10, 2017 at 4:35

2 Answers 2

2

change your function to store the base64 value

$(document).ready(function(){
    $("input[name=property_images]").change(function() {
      var imgBase64Arr = [];
      var files = this.files;
      for (var i = 0; i < files.length; i++) {
        (function(i){
          var FR = new FileReader();
          FR.onload = function(e) {
            imgBase64Arr.push( e.target.result );//adding base64 value to array

            if(i === files.length -1)//after all files are porcessed
              submitData(imgBase64Arr)
          };
          FR.readAsDataURL(files[i]);
        })(i);
      }

    });

   function submitData(imgBase64Arr){
     console.log(imgBase64Arr);
   }
});
13
  • I tried your code Uncaught TypeError: Cannot read property '2' of undefined at FileReader.FR.onload Commented Jan 10, 2017 at 4:52
  • I tried now single file Uncaught TypeError: Cannot read property '1' of undefined at FileReader.FR.onload Commented Jan 10, 2017 at 4:58
  • Mr azad , possible means make fiddle answer Commented Jan 10, 2017 at 4:58
  • @SagunthalaK isn't helpful to you?
    – Azad
    Commented Jan 10, 2017 at 5:18
  • @Kaiido it is just display the contents of the array after processed all images
    – Azad
    Commented Jan 10, 2017 at 5:34
0

Here we have single image to convert base64 string.

function readFile() {
  if (this.files) {
    for(i=0;i<this.files.length;i++)
      {
    var FR= new FileReader();
    FR.onload = function(e) {
      //document.getElementById("img").src       = e.target.result;
      //document.getElementById("b64").innerHTML = e.target.result;
      //$("#b64").append("<div></div>").html(e.target.result);
      $('<img src="'+e.target.result+'" />').appendTo('#show-image');
      $('<p>'+e.target.result+'</p>').appendTo('#text-image');
    };       
    FR.readAsDataURL( this.files[i] );
  }
    }
}

document.getElementById("inp").addEventListener("change", readFile, false);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="inp" type='file' multiple>


<div id='show-image'></div>

<div id='text-image'></div>

3
  • I need multiselect image Commented Jan 10, 2017 at 4:53
  • 1
    @Vijay don't just copy paste others answers
    – Azad
    Commented Jan 10, 2017 at 5:14
  • @azad Sorry for that. Now is it ok? Commented Jan 10, 2017 at 5:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.