Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

download attribute not suggesting file extension in save dialog

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');
like image 985
Get Off My Lawn Avatar asked Oct 27 '25 23:10

Get Off My Lawn


1 Answers

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"
  });
});
like image 167
Turtlefight Avatar answered Oct 30 '25 15:10

Turtlefight



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!