問題描述
我正在使用下載鏈接,在電子中,鏈接打開,但 Save as type
只顯示 All Files (*.*)
有沒有辦法使用電子僅使用 <a>
標記在該字段中強制文件擴展名?這在顯示 MY_EXTENSION (*.my_extension)
的 chrome 中有效,但在電子中卻沒有.如果您在新名稱中重命名沒有擴展名的文件,這很有用,下載時會自動添加它.
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.
鏈接如下:
<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');
推薦答案
你可以使用 DownloadItem
在你的電子主進程中攔截下載.
You can use DownloadItem
in your main process in electron to intercept the download.
然后你可以調用 downloadItem.setSaveDialogOptions
修改電子將顯示的保存對話框.
Then you can call downloadItem.setSaveDialogOptions
to modify the save dialog that will be displayed by electron.
在保存選項中,您可以指定 FileFilters
,它將控制用戶在保存文件時可以選擇哪些擴展名.
In the save options you can specify the FileFilters
which will control from which extensions the user can choose when saving the file.
例子:
// 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"
});
});
這篇關于下載屬性在保存對話框中不建議文件擴展名的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!