I want a cleaner UI than what
<input type="file" ...>
gives me.
I can use window.showOpenFilePicker() to get the user gesture security context and get a "fileSystemHandle" from that API but I can't figure out how to read that file using the FileReader object. (or any other way)
Here is my failing code:
/**
* Used to import via a simple button.
* The caller must call window.showOpenFilePicker() and give the
* fileHandle to this API.
* @param {*} fileHandle
* @param {*} fnImportToUI
*/
fio.importFromFileButton = async function(fileHandle, fnImportToUI) {
if (Array.isArray(fileHandle)) {
fileHandle = fileHandle[0];
}
var blob;
const reader = new FileReader();
reader.addEventListener('loadend', (event) => {
blob = new Blob([new Uint8Array(event.target.result)], {type: fileHandle.type })
fnImportToUI(fileHandle, event.target.result)
});
reader.readAsArrayBuffer(blob);
}
The problem being that the fileSystemHandle is not a Blob (I get an error if I call
reader.readAsArrayBuffer(fileHandle);
or
reader.readAsText(fileHandle);
or using any of the ReadFile read APIs.)
From what I can tell, I can't convert the fileSystemHandle to a Blob until I have read the file to get the data - a chicken and egg issue.
getFileon theFileSystemFileHandlein the Promise returned byshowOpenFilePickerand thenawaitthat Promise to get theFileobject (which N.B. is already a Blob). See this and this and this for more detail.