javascript - HTML5 FileReader equivalent for nodejs 'fs' - Stack Overflow

I was trying to read and write a file in javascript. Nodejs 'fs'is not working. I came to kno

I was trying to read and write a file in javascript. Nodejs 'fs'is not working. I came to know that there is an equivalent HTML5 API's.

What is the equivalent for

fs.writeFile('my.file', filedata)
fs.readFileSync(__dirname + '/dir/leaves.file')

in HTML5 filereader api

I was trying to read and write a file in javascript. Nodejs 'fs'is not working. I came to know that there is an equivalent HTML5 API's.

What is the equivalent for

fs.writeFile('my.file', filedata)
fs.readFileSync(__dirname + '/dir/leaves.file')

in HTML5 filereader api

Share Improve this question asked Aug 30, 2017 at 6:23 SibirajSibiraj 4,7668 gold badges37 silver badges59 bronze badges 4
  • You are looking for the FileReader: developer.mozilla/en-US/docs/Web/API/FileReader – Get Off My Lawn Commented Aug 30, 2017 at 6:24
  • can you tell me an exact equivalent? I am little confused @GetOffMyLawn – Sibiraj Commented Aug 30, 2017 at 6:29
  • There is no* fs equivalent in web APIs, for obvious security reasons. fs-> filesystem, and browsers won't give access to the user's filesystem to any random script on the web. You can read files with a FileReader, you can load files from a server with XHR, or you can ask your user to give you files from their f-s, and you can prompt your user to save files. But none of these operations will be done directly from the user's file-system, without his action. (*well actually we could consider IndexedDB and alike as filesystems...) – Kaiido Commented Aug 30, 2017 at 7:41
  • @Kaiido . Can you post that as an answer? I will close this question. – Sibiraj Commented Oct 30, 2017 at 5:25
Add a ment  | 

2 Answers 2

Reset to default 2

There is no* fs equivalent in web APIs, for obvious security reasons.

fs -> filesystem, and browsers won't give access to the user's filesystem to any random script on the web.

  • You can read files with a FileReader,
  • you can load files from a server with XHR, or you can ask your user to give you files from their filesystem,
  • and you can prompt your user to save files.

But none of these operations will be done directly from the user's file-system, without his own action.

(*Actually we could consider IndexedDB and alike as filesystems, but they can't be pared to fs either...)

You can load files using the FileReader like so:

var openFile = function(event) {
    var input = event.target;

    var reader = new FileReader();
    reader.onload = function(){
        var dataURL = reader.result;
        var output = document.getElementById('output');
        output.src = dataURL;
    };
    reader.readAsDataURL(input.files[0]);
};
<input type='file' accept='image/*' onchange='openFile(event)'><br>
<img id='output'>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745631142a4637115.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信