【问题标题】:Write a file to a specific directory with neutralinojs使用中性节点将文件写入特定目录
【发布时间】:2020-06-26 18:52:44
【问题描述】:

我使用网页来创建一个带有 javascript 的 xml 文件。 我想用中性点将 xml 字符串写入我计算机上特定目录中的文件中。 是否可以打开文件保护浏览器或直接在代码中指定要保存的文件的路径(或当前目录)。

谢谢。

【问题讨论】:

    标签: neutralinojs


    【解决方案1】:

    需要指定目录,文件名如官方示例所示:

    Neutralino.filesystem.writeFile('D:\\files\\librairies\\Desktop\\file1.txt','hello world',
        function (data) {
            console.log(data);
        },
        function () {
            console.error('error');
        }
    );
    

    【讨论】:

      【解决方案2】:

      您还可以在写入文件之前打开文件保护程序对话框。以下代码将打开一个文件保护对话框:

      let response = await Neutralino.os.showDialogSave({
        title: 'Save to file'
      });
      

      您可以使用响应写入文件:

      //response.selectedEntry holds the chosen file path and name (e.g. "C:/TEST/myFile.txt")
      await Neutralino.filesystem.writeFile({
        fileName: response.selectedEntry,
        data: 'my file content'
      });
      

      您必须将该代码放在异步函数中。

      完整代码:

      async function saveMyFile()
        let response = await Neutralino.os.showDialogSave({
          title: 'Save to file'
        });
        await Neutralino.filesystem.writeFile({
          fileName: response.selectedEntry,
          data: 'my file content'
        });
      }
      
      saveMyFile();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-02
        • 2019-06-16
        • 1970-01-01
        • 2013-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多