no_file_data 松弛文件上传中的响应

no_file_data response in slack file upload

我正在使用 node.js 尝试通过 slackAPI 的上传文件方法上传 csv 文件。方法是post。我不确定如何做到这一点,因为如果我使用内容参数而不是文件,我会收到错误消息:

{ ok: false, error: 'invalid_array_arg' }

如果我使用文件参数,我仍然得到错误:

{ ok: false, error: 'invalid_array_arg' }

此代码中有多个错误点,我已尝试对每个错误点进行测试,但我确定我在这里遗漏了一些信息。这是我创建的 uploadFile 方法:

function uploadFile(file){
    console.log(botToken);
    axios.post('https://slack.com/api/files.upload', qs.stringify({token: botToken, file: file, channels: 'testing'}))
        .then(function (response) {
             var serverMessage = response.data;
             console.log(serverMessage);
             console.log("inside file upload function");
})
} 

我是这样调用方法的:

var file = fs.createReadStream(__dirname + '/' + csvFilePath);   // <--make sure this path is correct
    console.log(__dirname + '/' + csvFilePath);
    uploadFile(file);

最后输出:

机器人已经启动! C:\Users\i502153\WebstormProjects\slackAPIProject/accessLogs/CSV/1548430592860output.csv* { 好:假,错误:'invalid_array_arg' } 内部文件上传功能

我哪里做错了,如何纠正?

链接: https://api.slack.com/methods/files.upload https://www.npmjs.com/package/axios

您的解决方案将不起作用,因为您正试图获取一个 流对象 (file) 并将其字符串化为一个查询字符串,这将将无意义的字符串“[object]”插入到查询中。它实际上不会将数据流式传输到 Slack。

不幸的是,Axios 在节点中的工作方式与在浏览器中的工作方式不同,而且它们的文档可能有点令人困惑。

我会建议这样的方法(未经测试):

const axios = require('axios');
const FormData = require('form-data');

function uploadFile(file) {
    const form = new FormData();
    form.append('token', botToken);
    form.append('channels, 'testing');
    form.append('file', file, 'optionalfilenamehere');
    return axios.post('https://slack.com/api/files.upload', form, {
        headers: form.getHeaders()
    }).then(function (response) {
        var serverMessage = response.data;
        console.log(serverMessage);
        console.log('inside file upload function');
    });
}

我根据票证 https://github.com/axios/axios/issues/1006#issuecomment-320165427 中的建议改编了此代码,如果您 运行 遇到问题,那里可能还有其他有用的评论。祝你好运!

编辑:对于稍后阅读本文的人,对于使用 request 而不是 axios 的类似方法,请参阅相关问题