Search code examples
phpjqueryfile-uploadjquery-file-uploadblueimp

jQuery File Upload Plugin https://blueimp.net


Good evening, I would like to use the plugin jQuery File Upload Plugin but I have a problem. I'm using a base64 image, because the image passes through a trimming tool to cut the image. How can I upload and upload an image in base64 format via .fileupload () I need a jquery / php version

I can not find solutions, thanks in advance Sincerely


Solution

  • From their documentation: https://github.com/blueimp/jQuery-File-Upload/wiki/API#programmatic-file-upload

    $('#fileupload').fileupload('add', {files: filesList});
    

    The second argument must be an object with an array (or array-like list) of File or Blob objects as files property.

    So sounds like it is as simple as:

    var blob = ... //you said in the comments you've got the blob...
    var blob = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
    $('#fileupload').fileupload('add', {files: [blob]})
    

    Now, I've also come across some more complex code (source) that suggests something like this is also possible:

    $('#fileupload').fileupload({
        autoUpload: true,
        add: function (event, data) {
          $.ajax({
            url: "/upload",
            type: 'POST',
            dataType: 'json',
            data: {doc: {title: data.files[0].name}},
            async: false,
            success: function(response) {
              ...
            }
    
          });
    

    Seems pretty flexible.