Convert URL to File or Blob for FileReader.readAsDataURL

后端 未结 8 805
情话喂你
情话喂你 2020-11-27 13:57

Reference: FileReader.readAsDataURL

Considering the following example:

function previewFile(file) {

  var reader  = new FileReader();

  reader.onlo         


        
相关标签:
8条回答
  • 2020-11-27 14:36

    Try this I learned this from @nmaier when I was mucking around with converting to ico: Well i dont really understand what array buffer is but it does what we need:

    function previewFile(file) {
    
      var reader  = new FileReader();
    
      reader.onloadend = function () {
        console.log(reader.result); //this is an ArrayBuffer
      }
      reader.readAsArrayBuffer(file);
    }
    

    notice how i just changed your readAsDataURL to readAsArrayBuffer.

    Here is the example @nmaier gave me: https://stackoverflow.com/a/24253997/1828637

    it has a fiddle

    if you want to take this and make a file out of it i would think you would use file-output-stream in the onloadend

    0 讨论(0)
  • 2020-11-27 14:37

    I know this is an expansion off of @tibor-udvari's answer, but for a nicer copy and paste.

    async function createFile(url, type){
      if (typeof window === 'undefined') return // make sure we are in the browser
      const response = await fetch(url)
      const data = await response.blob()
      const metadata = {
        type: type || 'video/quicktime'
      }
      return new File([data], url, metadata)
    }
    
    0 讨论(0)
  • 2020-11-27 14:49

    This information is outdated as of now, but cannot be deleted.

    1. You can create File instances just by specifying a path when your code is chrome-privileged:

      new File("/path/to/file");
      

      File is a sub-class of Blob, so all File instances are also valid Blobs. Please note that this requires a platform path, and not a file URL.

    2. Yes, FileReader is available to addons.

    File and FileReader are available in all windows. If you want to use them in a non-window scope (like bootstrap.js or a code module), you may use nsIDOMFile/nsIDOMFileReader.

    0 讨论(0)
  • 2020-11-27 14:50

    To convert a URL to a Blob for FileReader.readAsDataURL() do this:

    var request = new XMLHttpRequest();
    request.open('GET', MY_URL, true);
    request.responseType = 'blob';
    request.onload = function() {
        var reader = new FileReader();
        reader.readAsDataURL(request.response);
        reader.onload =  function(e){
            console.log('DataURL:', e.target.result);
        };
    };
    request.send();
    
    0 讨论(0)
  • 2020-11-27 14:51

    Here is my code using async awaits and promises

    const getBlobFromUrl = (myImageUrl) => {
        return new Promise((resolve, reject) => {
            let request = new XMLHttpRequest();
            request.open('GET', myImageUrl, true);
            request.responseType = 'blob';
            request.onload = () => {
                resolve(request.response);
            };
            request.onerror = reject;
            request.send();
        })
    }
    
    const getDataFromBlob = (myBlob) => {
        return new Promise((resolve, reject) => {
            let reader = new FileReader();
            reader.onload = () => {
                resolve(reader.result);
            };
            reader.onerror = reject;
            reader.readAsDataURL(myBlob);
        })
    }
    
    const convertUrlToImageData = async (myImageUrl) => {
        try {
            let myBlob = await getBlobFromUrl(myImageUrl);
            console.log(myBlob)
            let myImageData = await getDataFromBlob(myBlob);
            console.log(myImageData)
            return myImageData;
        } catch (err) {
            console.log(err);
            return null;
        }
    }
    
    export default convertUrlToImageData;
    
    0 讨论(0)
  • 2020-11-27 14:56

    Add cors mode to prevent files cors blocked

    fetch(url,{mode:"cors"})
                        .then(res => res.blob())
                        .then(blob => {
                            const file = new File([blob], 'dot.png', {type:'image/png'});
                            console.log(file);
                        });
    
    0 讨论(0)
提交回复
热议问题