Convert data file to blob

后端 未结 3 1783

How to get a blob?

HTML:


JavaScript:

function previewFile()          


        
3条回答
  •  温柔的废话
    2021-01-03 22:41

    As pointed in the comments, file is a blob:

    file instanceof Blob; // true
    

    And you can get its content with the file reader API https://developer.mozilla.org/en/docs/Web/API/FileReader

    Read more: https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications

    var input = document.querySelector('input[type=file]');
    var textarea = document.querySelector('textarea');
    
    function readFile(event) {
      textarea.textContent = event.target.result;
      console.log(event.target.result);
    }
    
    function changeFile() {
      var file = input.files[0];
      var reader = new FileReader();
      reader.addEventListener('load', readFile);
      reader.readAsText(file);
    }
    
    input.addEventListener('change', changeFile);
    
    

提交回复
热议问题