【发布时间】:2018-02-06 20:51:08
【问题描述】:
我正在尝试使用 node.js 和 request package 通过 Slack 上传图像,但运气不佳。我从 API 收到 invalid_array_arg 或 no_file_data 错误。
这是我的要求:
var options = { method: 'POST',
url: 'https://slack.com/api/files.upload',
headers:
{ 'cache-control': 'no-cache',
'content-type': 'application/x-www-form-urlencoded' },
form:
{ token: SLACK_TOKEN,
channels: SLACK_CHANNEL,
file: fs.createReadStream(filepath)
} };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
我看了一些相关的帖子:
- Can I upload an image as attachment with Slack API?
- Slack API (files.upload) using NodeJS
- fix files.upload from Buffer with formData options #307
唯一有效的是直接使用 curl 命令,但使用 cygwin(CommandPrompt 失败:curl: (1) Protocol https not supported or disabled in libcurl)。从节点调用 curl 的问题(使用 child_process)但在命令提示符中静默失败,仍然使用 cygwin 返回 no_file_data(将绝对路径传递给文件):
stdout: {"ok":false,"error":"no_file_data"}
stderr: % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 469 100 35 100 434 359 4461 --:--:-- --:--:-- --:--:-- 6112
我在 Windows 上使用节点 v6.9.1。
我错过了什么?如何在 Windows 上通过 node.js 将图像上传到 slack?
【问题讨论】:
-
我知道这不是您问题的答案,也许您应该考虑使用node-slack-sdk/,在这种情况下您不需要自己实现这些功能。 file upload documentation
-
@Peter 使用 node-slack-sdk:
error: Response not OK: no_file_data Error: no_file_data出人意料地得到了同样的错误 -
@GeorgeProfenza 要走的路是files.upload。
-
为什么您的 #2 链接问题的解决方案不适合您? (stackoverflow.com/questions/38084459/…) 你试过用同样的语法重现吗?
-
@Tanaike 我希望我能给你不止一票。两种解决方案都有效!与此同时,我进行了更多测试并发现了问题:我尝试在图像完全写入磁盘之前上传图像。一旦我确保文件完全写入磁盘,第二种方法也可以使用。有趣的是,即使图像没有完全写入磁盘,您的第一种方法(手动创建多部分标头)也能正常工作:它只是为丢失的数据显示灰色像素。
标签: javascript node.js file-upload slack slack-api