2015-09-04 157 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

相關問題