如何使用nodejs在文件系统中使用实际文件路径?

时间:2016-11-01 15:48:31

标签: javascript node.js file-upload filesystems

我正在从客户端向服务器发送文件,以便我可以阅读内容,所以当我尝试上传文件时,我收到错误cannot read property,我抓了几个小时但是找不到解决方案。看起来像路径问题任何想法如何解决这个问题?

server.js

export function create(req, res) {
 data.parse(req, function(err, fields, files) {
    var fileContent = fs.readFileSync(files.file[0].path,'utf8');
     res.json(fileContent   );
    });

console.log(files.file)

[ { fieldName: 'file',
originalFilename: 'sco_poc.bpmn',
path: 'C:\\Users\\529u\\AppData\\Local\\Temp\\R8CtG_jsOxhkEvYiGlfsGolM.bpmn',
headers:
 { 'content-disposition': 'form-data; name="file"; filename="sco_poc.bpmn"',
   'content-type': 'application/octet-stream' },
size: 11078 } ]

错误

sers\sh529u\modeler\modeler\server\api\fileUpload\fileUpload.controller.js:14
    var fileContent = fs.readFileSync(files.file[0].path, 'utf8');
                                                ^

TypeError: Cannot read property '0' of undefined
    at C:/modeler/server/api/fileUpload/fileUpload.controller.js:7:46
    at C:\Users\sh529u\modeler\modeler\node_modules\multiparty\index.js:129:9
    at C:\Users\sh529u\modeler\modeler\node_modules\multiparty\index.js:108:9
    at nextTickCallbackWith0Args (node.js:420:9)
    at process._tickCallback (node.js:349:13)

0 个答案:

没有答案