как загрузить base64 str с помощью plupload - PullRequest
0 голосов
/ 07 мая 2018
  • Я выполняю преобразование строки base64 в объект File и добавляю объект в «объект загрузки» в методе addFiled.

    'upload.files.length === 0'после upload.addFile (fileObj);

  • Я не могу настроить параметры.если я использую upload.setOption ({url: 'xxx'}), будет выброшено сообщение об ошибке 'url of null'.

    var cans = document.getElementById('cans');
    var ctx = cans.getContext("2d");
    
    var img = document.getElementById('img');
    
    img.onload = function () {
    
        ctx.drawImage(img, 10, 10, 100, 100);
    
        var dataURL = cans.toDataURL();
    
        function dataURLtoFile(dataurl, filename) {
            var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
                bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
            while (n--) {
                u8arr[n] = bstr.charCodeAt(n);
            }
            return new File([u8arr], filename, { type: mime });
        }
    
        const fileObj = dataURLtoFile(dataURL, 'img.png');
    
        var uploader = new plupload.Uploader({
            runtimes: 'html5',
            url: 'http://oss.aliyuncs.com',
    
            init: {
                PostInit: function () {
    
                },
    
                FilesAdded: function (up, files) {
                   console.log(files,33);
                },
    
                FileUploaded: function (up, file, info) {
                    if (info.status == 200) {
                        alert('success');
                    }
                    else {
                        alert('error');
                    }
                },
    
                Error: function (up, err) {
                    console.log(err);
                }
            }
        });
    
        uploader.init();
    
        document.getElementById('uploadBtn').onclick = function () {
    
            uploader.addFile(fileObj,'');
            console.log(uploader.files);// [ ];
    
            //uploader.start();
        }
    
    }
    
...