如何通过plupload上传base64 str

时间:2018-05-07 06:20:09

标签: plupload

  • 我将base64字符串转换为File对象,然后添加该对象 在methond addFiled中“上传对象”。

    在upload.addFile(fileObj)之后

    'upload.files.length === 0';

  • 我无法配置参数。如果我使用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();
        }
    
    }
    

0 个答案:

没有答案