JavaScript file to byte[]

前端 未结 5 1748
野趣味
野趣味 2021-01-12 14:14

Is it possible to read a file in from a path in JavaScript and create a byte[] of that file?

5条回答
  •  情歌与酒
    2021-01-12 14:48

    Yes, you can — in Firefox, anyway. Other browsers may or may not choose to allow it in the future.

    Make a file upload field for the user to pick the file, and read it through the input.files list. eg. document.getElementById('myuploadfield').files[0].getAsBinary(). This puts each byte in a single character of a JavaScript String, which is about as close to a byte[] as you're going to get.

    This is quite a specialized interface and probably Not The Right Thing — heed the other replies, because it's very possible you are trying to do something in a inappropriate way. Difficult to tell without context.

提交回复
热议问题