有什么办法可以更改电子文件对话框的标题和保存类型?
Is there any way to change the title and save type of file dialog on electron?
我创建了一个按钮,用于下载和保存从 api 端点返回的文件。
在普通浏览器中,点击按钮后,会出现保存文件对话框,标题Save As
和Save as type
按文件扩展名显示。但是在 electron
中,标题似乎是一个文件 url(在我的例子中它显示 blob://https:...
因为我的是用 URL.createObjectURl
创建的)。
那么我是否需要将任何选项设置为 a
标记以使对话框标题为 Save As
并更正文件的保存类型(不使用 electron 的本机对话框)?
...
<a hidden href='/' ref={downloadRef}>download</a>
<button onClick={handleSaveFile}>download</button>
...
const handleSaveFiles = (file: Blob, fileName: string): void => {
const fileDownloadUrl = window.URL.createObjectURL(file);
if (downloadRef.current) {
downloadRef.current.href = fileDownloadUrl;
downloadRef.current.download = fileName;
downloadRef.current.click();
}
};
实际上,点击那个按钮打开的对话框已经是electron的对话框,然后我可以通过will-download
事件编辑标题和过滤器。
...
this.browserWindow = new BrowserWindow(option);
...
this.browserWindow.webContents.session.on('will-download', (event, item) => {
let filters = [];
switch (item.getMimeType()) {
case 'text/csv':
filters = [{ name: 'Microsoft Excel Comma Separated Values File', extensions: ['csv'] }];
break;
case 'application/octet-stream':
filters = [{ name: 'Zip archive', extensions: ['zip'] }];
break;
default:
break;
}
item.setSaveDialogOptions({
title: 'Save As',
filters: [...filters, { name: 'All Files', extensions: ['*'] }],
});
});
参考https://www.electronjs.org/docs/latest/api/download-item#downloaditemsetsavedialogoptionsoptions
我创建了一个按钮,用于下载和保存从 api 端点返回的文件。
在普通浏览器中,点击按钮后,会出现保存文件对话框,标题Save As
和Save as type
按文件扩展名显示。但是在 electron
中,标题似乎是一个文件 url(在我的例子中它显示 blob://https:...
因为我的是用 URL.createObjectURl
创建的)。
那么我是否需要将任何选项设置为 a
标记以使对话框标题为 Save As
并更正文件的保存类型(不使用 electron 的本机对话框)?
...
<a hidden href='/' ref={downloadRef}>download</a>
<button onClick={handleSaveFile}>download</button>
...
const handleSaveFiles = (file: Blob, fileName: string): void => {
const fileDownloadUrl = window.URL.createObjectURL(file);
if (downloadRef.current) {
downloadRef.current.href = fileDownloadUrl;
downloadRef.current.download = fileName;
downloadRef.current.click();
}
};
实际上,点击那个按钮打开的对话框已经是electron的对话框,然后我可以通过will-download
事件编辑标题和过滤器。
...
this.browserWindow = new BrowserWindow(option);
...
this.browserWindow.webContents.session.on('will-download', (event, item) => {
let filters = [];
switch (item.getMimeType()) {
case 'text/csv':
filters = [{ name: 'Microsoft Excel Comma Separated Values File', extensions: ['csv'] }];
break;
case 'application/octet-stream':
filters = [{ name: 'Zip archive', extensions: ['zip'] }];
break;
default:
break;
}
item.setSaveDialogOptions({
title: 'Save As',
filters: [...filters, { name: 'All Files', extensions: ['*'] }],
});
});
参考https://www.electronjs.org/docs/latest/api/download-item#downloaditemsetsavedialogoptionsoptions