Son*_*tty 5 javascript node.js
我正在尝试读取放置在 Visuals Studio 2017 项目文件夹中的文件。由于文件路径中有空格,下面的代码总是返回'Error - File does not Exists'。我尝试在路径周围加上双引号,并将所有空格替换为 %20。不幸的是,这两种方法都不适合我
有人可以帮我解决吗?
文件路径 -
C:/Users/Guest/Documents/Visual Studio 2017/Projects/help.txt
Run Code Online (Sandbox Code Playgroud)
代码:
if (fs.existsSync(filePath)) {
fs.readFile(filePath, 'utf8', function (err, contents) {
if (err) {
logger.error("Error while reading the file - " + filePath)
next("Error while reading the file")
} else {
next(contents);
}
});
} else {
logger.error("File does not exist in path - " + filePath)
next("Error - File does not Exists")
}
Run Code Online (Sandbox Code Playgroud)
转到您的目录C:/Users/Guest/Documents/并dir /x在控制台中执行命令。您将获得文件夹的快捷名称Visual Studio 2017,如下所示:
02-11-2015 22:50 <DIR> VISUAL~1 Visual Studio 2005
05-11-2015 20:40 <DIR> VISUAL~2 Visual Studio 2008
27-01-2016 23:35 <DIR> VISUAL~3 Visual Studio 2010
13-08-2017 00:42 <DIR> VISUAL~4 Visual Studio 2012
04-02-2017 00:02 <DIR> VI3A49~1 Visual Studio 2013
15-10-2017 02:06 <DIR> VIDE5F~1 Visual Studio 2015
Run Code Online (Sandbox Code Playgroud)
读取时在文件路径中使用您想要的快捷方式名称。
另一个解决方法是转义空格字符。
path = filePath.split(/\ /).join('\ ');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5269 次 |
| 最近记录: |