查找相对文件path来parsingExcel文件

我正在使用Node.JS构build一个Web应用程序,至less应该允许用户上传Excel电子表格(.xlsx),然后使用Excelparsing器(目前使用node-xlsx – https://www.npmjs.org / package / node-xlsx ),我希望能够find这个文件,parsing它,并将其内容打印到控制台。 到目前为止,我已经上传并存储了文件,但是在指定我的应用程序应该search的文件path时遇到了问题。

我相信我的麻烦是,我试图在服务器端做到这一点,我告诉我的应用程序无法访问时通过用户目录search此文件。

以下是示例代码:

var fullfile; app.post('/upload', function (request, response) { var fstream; request.pipe(request.busboy); request.busboy.on('file', function (fieldname, file, filename) { console.log('Uploading: ' + filename); fstream = fs.createWriteStream('./storedFiles/' + filename); file.pipe(fstream); fstream.on('close', function () { response.redirect('success'); console.log('Uploaded to ' + fstream.path); fullfile=fstream.name; var obj = xlsx.parse(__dirname + fullfile); console.log(obj); }); }); }); 

这会产生错误:

返回binding.open(pathmodule._makelong(path)stringtoflags(标志)模式)错误:ENOENT,没有这样的文件或目录'C \ Users(我的本地计算机上的文件path)

任何人都可以指出一个这样做,我失踪了吗? 它与我感觉的FS方法有关。

谢谢

首先,不要使用用户在保存文件时提供的文件名 – 你会得到重复的,这可能是一个安全风险(一般来说,不要信任用户提供的数据)。 只需使用自己的值 – 使用自己的命名约定来防止重复或使用操作系统提供的tmp文件更为标准。

要解决您的问题,请尝试:

需要在文件顶部的path

 var path = require('path'); 

并将fullfile的值更改为:

 fullfile = path.join(__dirname,fstream.path) 

然后将fullfile传递给xlsx.parse

 var obj = xlsx.parse(fullfile); 
Interesting Posts