繁体   English   中英

有没有办法在电子上更改标题和保存文件对话框的类型?

[英]Is there any way to change the title and save type of file dialog on electron?

我创建了一个按钮,用于下载和保存从 api 端点返回的文件。

在普通浏览器中,点击该按钮后,会出现一个保存文件对话框,标题为Save AsSave as type由文件扩展名显示。 但是在electron ,标题似乎是一个文件 url (在我的例子中它显示blob://https:...因为我的是用URL.createObjectURl创建的)。

所以,是的任何选项我需要设置a标记,让对话框的标题为Save As和纠正保存的文件类型(不使用电子的原始对话)?

...
<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();
  }
};

实际上,单击该按钮时打开的对话框已经是电子的对话框,然后我可以通过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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM