问题
I am using a download link and in electron, the link opens but the Save as type
only shows All Files (*.*)
Is there a way for electron to force a file extension in that field using just an <a>
tag? This works in chrome where it shows MY_EXTENSION (*.my_extension)
, but in electron it does not. This is useful for if you rename the file without the extension in the new name, it will automatically add it when downloaded.
Here is what the link looks like:
<a href="/path/to/file.my_extension" download>Download File</a>
Here is what the server response looks like:
res.set('Content-disposition', 'attachment; filename=' + req.params.name + '.my_extension');
res.set('Content-Type', 'application/zip');
回答1:
You can use DownloadItem in your main process in electron to intercept the download.
Then you can call downloadItem.setSaveDialogOptions to modify the save dialog that will be displayed by electron.
In the save options you can specify the FileFilters
which will control from which extensions the user can choose when saving the file.
Example:
// in your main process:
const { BrowserWindow } = require('electron');
// create the default window
let win = new BrowserWindow();
// handle download event
win.webContents.session.on('will-download', (event, item, webContents) => {
// TODO: find out what the user is downloading and set options accordingly
item.setSaveDialogOptions({
filters: [
// Set your allowed file extensions here
{name: "My Special Filter", extensions: ["special"]},
{name: "Images", extensions: ["jpg", "png"]
],
message: "Please pick your poison"
});
});
来源:https://stackoverflow.com/questions/59253771/download-attribute-not-suggesting-file-extension-in-save-dialog