Browserify with require('fs')

前端 未结 5 1136
感动是毒
感动是毒 2020-11-28 11:24

I was trying to use browserify on a file that uses the fs object. When I browserify it, the call to require(\'fs\') doesn\'t get transformed and require

5条回答
  •  没有蜡笔的小新
    2020-11-28 11:49

    Is it necessary for you to use require (fs) , you could always use html5 file reader api to read files in javascript.

    window.onload = function() {
        var fileInput1 = document.getElementById('fileInput');
        if (fileInput1){
            fileInput1.addEventListener('change', function(e) {
                var file = fileInput1.files[0];
                var textType = /text.*/;
    
                if (file.type.match(textType)) {
                    var reader = new FileReader();
                    reader.onload = function(e) {
                        console.log(reader.result);
                      }    
                    reader.readAsText(file);                    
                } 
            });
        }
    }
    

    you will also have to insert a input file in the html side.

提交回复
热议问题