ios10 base64转为file文件 就无法通过formdata上传 上传的file为空

直接转为file就无法上传 原因未知
通过先转为blob在转为file,问题解决

function dataURLtoFile(dataurl, filename) {
    //转成blob
    function dataURLtoBlob(toDataURL) {
        var arr = toDataURL.split(","),
            mime = arr[0].match(/:(.*?);/)[1],
            bstr = atob(arr[1]),
            n = bstr.length,
            u8arr = new Uint8Array(n);
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n);
        }

        var blob = null;

        // 解决低版本浏览器没有new Blob()问题
        try {
            blob = new Blob([u8arr], { type: mime });
        } catch (e) {
            var BlobBuilder = window.BlobBuilder = (
                window.BlobBuilder ||
                window.WebKitBlobBuilder ||
                window.MozBlobBuilder ||
                window.MSBlobBuilder
            );

            if (e.name === "TypeError" && BlobBuilder) {
                var builder = new BlobBuilder();
                builder.append(u8arr);
                blob = builder.getBlob(mime);
            }
        }

        return blob;
    } 

    //转成file
    function blobToFile(Blob, fileName) {
        Blob.lastModifiedDate = new Date();
        Blob.name = fileName;
        return Blob;
    }

    // 调用如下
    let bold = dataURLtoBlob(dataurl);
    let file = blobToFile(bold, "fileName");
    
    // 上传file就可以了
    return file;
}

你可能感兴趣的:(IOS)