电子-在打开保存对话框之前写入文件

新别杰夫

我正在使用电子来开发应用程序。完成一些加密操作后,我需要向用户显示一个对话框来保存文件。我想要提供给文件的文件名是一个随机哈希,但与此同时我也没有成功。我正在尝试使用此代码,但不会保存该文件。我该如何解决?

const downloadPath = app.getPath('downloads')

ipcMain.on('encryptFiles', (event, data) => {
  let output = [];
  const password = data.password;
  data.files.forEach( (file) => {
    const buffer = fs.readFileSync(file.path);
    const dataURI = dauria.getBase64DataURI(buffer, file.type);
    const encrypted = CryptoJS.AES.encrypt(dataURI, password).toString();
    output.push(encrypted);
  })
  const filename = hash.createHash('md5').toString('hex');
  console.log(filename)
  const response = output.join(' :: ');
  dialog.showSaveDialog({title: 'Save encrypted file', defaultPath: downloadPath }, () => {
    fs.writeFile(`${filename}.mfs`, response, (err) => console.log(err) )  
  })
})
亚历山大·莱特纳

您遇到的问题是由Electron的UI函数的异步特性引起的:它们不采用回调函数,而是返回promise。因此,您不必传递回调函数,而只需处理promise的解决方案。请注意,这仅适用于Electron> =版本6。但是,如果您运行的是旧版本的Electron,则您的代码将是正确的-但您实际上应该更新到较新的版本(Electron v6已于一年前发布)。 。

像下面那样修改您的代码可以作为解决问题的起点。但是,由于您未说明如何生成哈希值(哈希值hash.createHash从何而来?;是否忘记声明/导入hash?;是否忘记传递任何消息字符串?;是否将hash其用作NodeJScrypto模块的别名?) ,因此(目前)无法调试为什么不从中得到任何输出console.log (filename)(我假设您的意思是“在代码中,将不会创建随机文件名”)。一旦您提供有关此问题的更多详细信息,我们将很乐意相应地更新此答案。

至于默认文件名:根据Electron文档,您可以传递文件路径以为dialog.showSaveDialog ()用户提供默认文件名。

实际上,您所使用的文件类型扩展名也应该与文件扩展名一起传递到保存对话框中。同样,将此文件扩展名作为过滤器传递到对话框中,也会阻止用户选择任何其他文件类型,最终通过将其附加到文件名来最终实现您的当前操作。

另外,您可以利用CryptoJS生成文件名:给定某个任意字符串,它实际上可以是随机字节,您可以这样做:filename = CryptoJS.MD5 ('some text here') + '.mfs';但是,请记住要明智地选择输入字符串。MD5已损坏,因此不应再用于存储秘密-使用对存储文件的加密至关重要的任何已知信息(例如data.password)本质上是不安全的。关于如何在Internet上的JavaScript中创建随机字符串,这里有一些很好的例子,在SO上也此答案

考虑到所有这些问题,可能会得到以下代码:

const downloadPath = app.getPath('downloads'),
      path = require('path');

ipcMain.on('encryptFiles', (event, data) => {
  let output = [];
  const password = data.password;

  data.files.forEach((file) => {
    const buffer = fs.readFileSync(file.path);
    const dataURI = dauria.getBase64DataURI(buffer, file.type);
    const encrypted = CryptoJS.AES.encrypt(dataURI, password).toString();
    output.push(encrypted);
  })

  // not working:
  // const filename = hash.createHash('md5').toString('hex') + '.mfs';

  // alternative requiring more research on your end
  const filename = CryptoJS.MD5('replace me with some random bytes') + '.mfs';
  console.log(filename);

  const response = output.join(' :: ');
  dialog.showSaveDialog(
    {
      title: 'Save encrypted file',
      defaultPath: path.format ({ dir: downloadPath, base: filename }), // construct a proper path
      filters: [{ name: 'Encrypted File (*.mfs)', extensions: ['mfs'] }] // filter the possible files
    }
  ).then ((result) => {
    if (result.canceled) return; // discard the result altogether; user has clicked "cancel"
    else {
      var filePath = result.filePath;
      if (!filePath.endsWith('.mfs')) {
        // This is an additional safety check which should not actually trigger.
        // However, generally appending a file extension to a filename is not a
        // good idea, as they would be (possibly) doubled without this check.
        filePath += '.mfs';
      }
      fs.writeFile(filePath, response, (err) => console.log(err) )  
    }
  }).catch ((err) => {
    console.log (err);
  });
})

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

电子:文件对话框窗口应重点放在决定之前

如何在电子中使用文件对话框?

电子从对话框读取文件得到类型错误

在打开对话框之前实例化TreeViewer

在“亲自”签名中使用“保存您的副本”对话框未收到电子邮件

在打开新对话框之前关闭对话框并显示新对话框会显示底层片段

电子:带有window.prompt的对话框

电子打印无对话框(静音打印)

电子对话框不会阻止与页面的交互

Streamreader 在打开的文件对话框中添加列

如何防止IE对话框“始终在打开此类型的地址之前询问”打开?

打开文件对话框后无法写入 tkinter spinbox

使用电子对话框模块和.showSaveDialog时,在“另存为”窗口中填充文件名

在电子中打开pdf文件

单击“按钮”后,在打开新活动之前显示警报对话框

打开新窗口-电子

xubuntu 18.04 打开/保存文件对话框,排序

旧样式的“打开/保存文件”对话框

AMP 电子邮件始终在 Gmail 网络应用程序中显示“放弃电子邮件草稿”对话框

带有消息框确认的电子应用程序关闭对话框

更改通过ActivityView打开的电子邮件对话框上的颜色颜色

下载文件(电子)

从对话框提示传回值以更新电子表格

电子对话框关闭会刷新我的当前页面

从渲染过程中要求电子对话框未定义

钛多个收件人电子邮件对话框

使用 Outlook 发送没有主题的电子邮件 --- 对话框问题

Google 电子表格对话框未显示输出

在模式对话框中创建按钮并在电子表格中运行代码