【问题标题】:sails: TypeError: path must be a string帆:TypeError:路径必须是字符串
【发布时间】:2015-09-04 10:11:08
【问题描述】:

我正在尝试使用 xls-to-json 节点模块将 xls 文件读取为 json 格式 发送文件路径时显示错误,例如路径必须是字符串

我在sn-p下面使用

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

请帮忙解决这个问题。

【问题讨论】:

  • typeof uploadedFiles[0].fd 返回什么?

标签: json node.js sails.js


【解决方案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);
      });
    });
  }
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 2020-08-12
    • 2017-05-18
    • 2020-10-21
    相关资源
    最近更新 更多