I have my own custom system that needs to create txt files in order to print cash receipts when there is a new order. I need those files to be saved locally to the computer to a specific location, but is there a way to save the file to that location without prompting the user to choose the file location? Is it possible if you create your own FileSystemFileHandle class and then pass it as a handle?
$('.save').click(function() {
saveToFile('from website');
});
async function saveToFile(content) {
const opts = {
type: 'save-file',
accepts: [
{
description: 'Text File',
extension: ['txt'],
mimeType: ['text/plain'],
}
]
}
const handle = await window.chooseFileSystemEntries(opts); // don't to that
// create custom FileSystemFileHandle and point to the file location?
const handle = FileSystemFileHandle;
const writable = await handle.createWritable();
await writable.write(content);
await writable.close();
}
This can't be done with the File System Access API, but ironically automatically triggered downloads are still a thing with the <a download>
approach.
const a = document.createElement('a');
// undefined
a.download = 'example.txt';
// "example.txt"
a.href = URL.createObjectURL(new Blob(['yolo'], {type : 'text/plain'}));
// "blob:https://example.com/8d494f54-499d-4f32-bdb4-ff047e8c60af"
a.click();
// undefined
// Downloads a file `example.txt` to your Downloads folder