問(wèn)題描述
我正在使用下載鏈接,在電子中,鏈接打開(kāi),但 Save as type
只顯示 All Files (*.*)
有沒(méi)有辦法使用電子僅使用 <a>
標(biāo)記在該字段中強(qiáng)制文件擴(kuò)展名?這在顯示 MY_EXTENSION (*.my_extension)
的 chrome 中有效,但在電子中卻沒(méi)有.如果您在新名稱中重命名沒(méi)有擴(kuò)展名的文件,這很有用,下載時(shí)會(huì)自動(dòng)添加它.
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>
這是服務(wù)器響應(yīng)的樣子:
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
在你的電子主進(jìn)程中攔截下載.
You can use DownloadItem
in your main process in electron to intercept the download.
然后你可以調(diào)用 downloadItem.setSaveDialogOptions
修改電子將顯示的保存對(duì)話框.
Then you can call downloadItem.setSaveDialogOptions
to modify the save dialog that will be displayed by electron.
在保存選項(xiàng)中,您可以指定 FileFilters
,它將控制用戶在保存文件時(shí)可以選擇哪些擴(kuò)展名.
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"
});
});
這篇關(guān)于下載屬性在保存對(duì)話框中不建議文件擴(kuò)展名的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!