The simplest approach would be to click or drag and drop the file at an <input type="file">
element.
<input type="file" accept="image/*">
<script>
const input = document.querySelector("input");
input.onchange = function(event) {
const file = event.target.files[0];
console.log(file);
}
</script>
If the file is served with CORS headers you can request the file using Image
constructor and pass the <img>
reference to canvas.toBlob()
, then pass Blob
reference to File
constructor, optionally set the name of the File
object.
Similarly CORS headers are necessary when using fetch()
and Response.blob()
to get Blob
of image.
Note if using Chrome or Chromium browser to request files at local filesystem you can launch Chrome or Chromium browsers with --allow-file-access-from-files
flag to access local filesystem at JavaScript and HTML, see Read local XML with JS.