简体   繁体   English

电子无法在Windows 10上写入文件

[英]Electron can't write files on windows 10

Using fs-extra to write files is failing on windows 10. The outputFile method is supposed to create any missing directory before writing, but it's throwing an error. 在Windows 10上,使用fs-extra写入文件失败。outputFile方法应该在写入之前创建所有丢失的目录,但是会引发错误。 I know the Downloads directory already exists, though. 我知道Downloads目录已经存在。

Error: ENOENT: no such file or directory, open 'C:\\Users\\josh\\Downloads\\2018-01-13_15:14:11.png' 错误:ENOENT:没有这样的文件或目录,打开'C:\\ Users \\ josh \\ Downloads \\ 2018-01-13_15:14:11.png'

const fs = require('fs-extra')
const app = require('electron').remote.app
const moment = require('moment')

let dataURL = this.$.canvas.toDataURL()
let filename = moment().format('YYYY-MM-DD_HH:mm:ss') + '.png'
let buffer = new Buffer(dataURL.replace('data:image/png;base64', ''), 'base64')
let filepath = path.resolve(app.getPath('downloads'), filename)
fs.outputFile(filepath, buffer, err => {
    if (err) {
        console.error(err)
    }
})

You cannot use colon ':' in the filename on Windows. 在Windows上,您不能在文件名中使用冒号':'。

From Naming Files, Paths, and Namespaces : 命名文件,路径和命名空间

Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following: 使用当前代码页中的任何字符作为名称,包括Unicode字符和扩展字符集(128–255)中的字符,但以下各项除外:

  • The following reserved characters: 以下保留字符:

    • < (less than) <(小于)
    • > (greater than) >(大于)
    • : (colon) :(冒号)
    • " (double quote) ”(双引号)
    • / (forward slash) /(正斜杠)
    • \\ (backslash) \\(反斜杠)
    • | | (vertical bar or pipe) (垂直杆或管)
    • ? (question mark) (问号)
    • * (asterisk) *(星号)
  • Integer value zero, sometimes referred to as the ASCII NUL character. 整数值零,有时也称为ASCII NUL字符。
  • Characters whose integer representations are in the range from 1 through 31, except for alternate data streams where these characters are allowed. 整数表示形式在1到31之间的字符,但允许这些字符的备用数据流除外。 For more information about file streams, see File Streams. 有关文件流的更多信息,请参见文件流。
  • Any other character that the target file system does not allow. 目标文件系统不允许的任何其他字符。

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

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