I'm downloading a file via javascript (the file is generated on the server) and I want to force the browser to prompt the user to choose the destination of the file. Is it possible? For instance, Chrome as an option with a default folder and I want to be able to override that option
EDIT (explaining the duplicate question) I think it's not a duplicate because I know why the dialog is not opening, just want to ask if the behavior can be overriden
I'm downloading a file via javascript (the file is generated on the server) and I want to force the browser to prompt the user to choose the destination of the file. Is it possible? For instance, Chrome as an option with a default folder and I want to be able to override that option
EDIT (explaining the duplicate question) I think it's not a duplicate because I know why the dialog is not opening, just want to ask if the behavior can be overriden
Share Improve this question edited Feb 8, 2016 at 10:32 NunoRibeiro asked Feb 8, 2016 at 10:07 NunoRibeiroNunoRibeiro 5112 gold badges8 silver badges23 bronze badges 1- 1 Possible duplicate of Prompting user to save file using a 'Save-as' dialog? – online Thomas Commented Feb 8, 2016 at 10:10
2 Answers
Reset to default 3You cannot override the download behaviour. A number of modern browsers save any download into a default location without prompting the user. That's how they download files, period. You cannot override this. It's a user choice in the browser's preferences whether they want to be prompted or not.
Yes, the behavior can be overridden using the File System Access API. The API exposes some methods that will override the browser's behavior.
Simply running window.showOpenFilePicker()
in the Chrome Browser console will launch the file picker prompt.
To override the download destination prompt behavior, you can make use of the window.showSaveFilePicker()
method.
// ...
const blob = new Blob(/*...*/);
// Use File System Access API
saveFileToDisk(blob, 'Some-File.txt')
async saveFileToDisk({blob, fileName}){
try {
const fileHandle = await self.showSaveFilePicker({
suggestedName: fileName,
types: [
{
description: "File",
// ...
},
],
});
const writeFile = async (fileHandle, contents) => {
// Create a FileSystemWritableFileStream to write to.
const writable = await fileHandle.createWritable();
// Write the contents of the file to the stream.
await writable.write(contents);
// Close the file and write the contents to disk.
await writable.close();
};
// write file
writeFile(fileHandle, blob).then(() => console.log("FILE DOWNLOADED!!!"));
} catch (error) {
console.log(error);
}
}
For more details, check out the documentation https://web.dev/file-system-access/ and https://web.dev/browser-fs-access/
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745473358a4629234.html
评论列表(0条)