[IOS] Android mobile end FileConstructor is not a constructor to solve the compatibility problems ios

A recent project, and if the pictures do upload photos, upload to file format. . So after taking a picture with a canvas to generate base64 format sub-file. . In the PC and Android is not a problem, not to IOS above. . After the new file is to generate a {};

Check the next file objects support the following browsers:
Here Insert Picture Description
interface documentation described above is also a special type of file blob:
Here Insert Picture Description
The blob object supports major browsers. Therefore, to convert then into blob file to upload, as follows:

//转成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);
      }
      return new Blob([u8arr], { type: mime });
    }
    //转成file
    function blobToFile(Blob, fileName) {
      Blob.lastModifiedDate = new Date();
      Blob.name = fileName;
      return Blob;
    }

    // 调用如下
    let bold = dataURLtoBlob(base64Url);
    let file = blobToFile(bold, "fileName");
    // 上传file就可以了
Published 134 original articles · won praise 80 · views 30000 +

Guess you like

Origin blog.csdn.net/Umbrella_Um/article/details/104467075