【问题标题】:Can't access files on network drive using FS npm module nodejs无法使用 FS npm 模块 nodejs 访问网络驱动器上的文件
【发布时间】:2021-07-30 09:09:20
【问题描述】:

我有一个映射到 X:\ 的网络驱动器,由于某种原因,我无法使用 FS 模块访问其中的文件。我尝试使用 fs.existsSync(file) 并返回找不到文件,但是当我输入路径 fs.existsSync('X:\path\to\file') 时,它工作正常。我硬编码的文件和字符串都完全相同,那么为什么 fs 会为两者返回不同的结果呢?我必须使用“文件”变量,因为如果它们不存在,我将遍历一组文件路径以在我的网络驱动器中创建它们。我还考虑了反斜杠,并将它们全部替换为双反斜杠,这样字符串就不会被转义。

    //This does not work
    let modifiedfilePathToSiger = path.join('X:\\', filePathToSiger.slice(17));
    let mps = JSON.stringify(modifiedfilePathToSiger)
    //Remove double quotes from mps
    let file = mps.replace(/['"]+/g, '')   //Ex: file here = X:\\Data\\Aerial Photo.docx
    
    let found = fs.existsSync(file);
    console.log(found) //Returns false

    //This does work
    let found = fs.existsSync('X:\\Data\\Aerial Photo.docx');
    console.log(found) //Returns true

另外需要注意的是,如果我在 fs.existsSync() 中输入整个硬编码路径,它可以正常工作并返回 true,但是如果我复制并粘贴从控制台记录的路径,它会返回 false,尽管完全相同的路径...没有一个字符是不同的。

【问题讨论】:

  • 会验证文件 === 'X:\\Data\\Aerial Photo.docx'?
  • 是的,我 console.log 文件的值,它返回相同的确切路径
  • 请原谅不清楚。 console.log(( file === 'X:\\Data\\Aerial Photo.docx')) 看看输出是不是true
  • 输出为假,这很奇怪,因为我记录了 typeOf(file) 并返回了 String。
  • 另外一点要补充的是,当使用 mime.lookup() 时,它能够在网络驱动器中找到文件。只有 FS 模块给我带来了问题。

标签: javascript node.js fs


【解决方案1】:

看起来当我记录文件变量时它返回 X:\Data\Aerial Photo.docx 但当我记录硬编码字符串 X:\Data\Aerial Photo.docx 时它返回 X:\Data\Aerial Photo.docx

使用.splitpath.win32 然后path.join

从节点 REPL

> path = require('path').win32
<ref *1> {
  resolve: [Function: resolve],
  normalize: [Function: normalize],
  isAbsolute: [Function: isAbsolute],
  join: [Function: join],
  relative: [Function: relative],
  toNamespacedPath: [Function: toNamespacedPath],
  dirname: [Function: dirname],
  basename: [Function: basename],
  extname: [Function: extname],
  format: [Function: bound _format],
  parse: [Function: parse],
  sep: '\\',
  delimiter: ';',
  win32: [Circular *1],
  posix: <ref *2> {
    resolve: [Function: resolve],
    normalize: [Function: normalize],
    isAbsolute: [Function: isAbsolute],
    join: [Function: join],
    relative: [Function: relative],
    toNamespacedPath: [Function: toNamespacedPath],
    dirname: [Function: dirname],
    basename: [Function: basename],
    extname: [Function: extname],
    format: [Function: bound _format],
    parse: [Function: parse],
    sep: '/',
    delimiter: ':',
    win32: [Circular *1],
    posix: [Circular *2],
    _makeLong: [Function: toNamespacedPath]
  },
  _makeLong: [Function: toNamespacedPath]
}
> strArr = file.split('\\')
[ 'X:', 'Data', 'Aerial Photo.docx' ]
> path.join(strArr[0],strArr[1],strArr[2])
'X:\\Data\\Aerial Photo.docx'
>

它会处理与环境相关的斜线

【讨论】:

  • 当我拆分文件时,它返回了以下数组 ['X:', '', 'Data', '', 'Aerial Photo.docx' ] 然后我做了 path.join(strArr[0 ], strArr[2], strArr[4]) 并使用该路径尝试了 fs.existsSync() 但仍然返回 false ('X:/Data/Aerial Photo.docx')
  • 更新了答案,请使用path.win32,nodejs.org/api/path.html#path_path_win32
  • 尝试使用 path.win32, file.split(path.sep); fs.existssync 仍然返回 false。
  • 另外由于某种原因它返回 ['X:', '', 'Data', '', 'Aerial Photo.docx' ] 而不是 ['X:', 'Data', '航拍照片.docx']
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-17
相关资源
最近更新 更多