Convert blob to base64

后端 未结 9 2025
无人共我
无人共我 2020-11-22 13:12

This is a snippet for the code that I want to do Blob to Base64 string:

This commented part works and that when the URL generated by this i

9条回答
  •  难免孤独
    2020-11-22 13:51

    So the problem is that you want to upload a base 64 image and you have a blob url. Now the answer that will work on all html 5 browsers is: Do:

      var fileInput = document.getElementById('myFileInputTag');
      var preview = document.getElementById('myImgTag');
    
      fileInput.addEventListener('change', function (e) {
          var url = URL.createObjectURL(e.target.files[0]);
          preview.setAttribute('src', url);
      });
    function Upload()
    {
         // preview can be image object or image element
         var myCanvas = document.getElementById('MyCanvas');
         var ctx = myCanvas.getContext('2d');
         ctx.drawImage(preview, 0,0);
         var base64Str = myCanvas.toDataURL();
         $.ajax({
             url: '/PathToServer',
             method: 'POST',
             data: {
                 imageString: base64Str
             },
         success: function(data) { if(data && data.Success) {}},
         error: function(a,b,c){alert(c);}
         });
     }
    

提交回复
热议问题