2015-09-04 158 views
0

我试图用xls-to-json节点模块 读取XLS文件,以JSON格式的字符串,而发送的文件路径它表明类似路径的错误必须是一个字符串帆:类型错误:路径必须是

我中号使用下面的代码片段

var fs = uploadedFiles[0].fd; 
//fs is a file path              
node_xj = require("xls-to-json"); 
node_xj({ 
      input:fs, 

     }, function(err, result) 
      { 
       if(err) 
       { 
        console.error(err); 
       } 
       else 
       { 
        console.log(result); 
       } 
      }); 

错误

fs.js:430 
    binding.open(pathModule._makeLong(path), 
     ^
TypeError: path must be a string 

请帮助解决这个问题。

+0

这是什么'的typeof UPLOADEDFILES [0] .fd'返回? –

+0

它返回字符串。 – Martin

回答

1

我的回答基于xls-to-json文档本身。假设您有接受文件作为输入文件的操作。输出是json文件在同一个文件夹中。因此,当您通过HTTP请求发送文件时,可以使用upload方法上传该文件。 files是一个包含上传文件的数组,您可以通过fd属性获取文件路径。

所以这个例子应该只是罚款:

// api/controllers/AnyController.js 
var path = require('path'); 
var xlsToJson = require('xls-to-json'); 

module.exports = { 
    index: function(req, res) { 
    req.file('param_name').upload(function(error, files) { 
     if (error) return res.serverError(error); 

     xlsToJson({ 
     input: files[0].fd, 
     output: path.resolve('./', 'output.json') 
     }, function(error, result) { 
     if (error) return res.serverError(error); 
     res.ok(result); 
     }); 
    }); 
    } 
}; 
+0

感谢它工作正常! – Martin

相关问题