【发布时间】:2022-01-11 23:43:35
【问题描述】:
我正在为我的 Strapi 应用程序构建一个 CSV 导入器,它的任务之一是从单元格中读取图像 URL,然后从 URL 下载并将其保存到媒体库。
代码如下所示:
const request = require('request').defaults({ encoding: null });
request.get(src, function (err, res, body) {
const fileName = src.split('/').pop();
strapi.plugins.upload.services.upload.upload({
files: {
path: body,
name: fileName,
type: res.headers['content-type'],
size: Number(res.headers['content-length']),
},
data: {
ref: 'products',
refId: data.itemID,
field: 'images'
}
});
});
下载成功,我在request.get 回调的body 变量中获得了一个缓冲区。但是将此传递给strapi.plugins.upload.services.upload.upload 会给我以下错误:
UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string or Uint8Array without null bytes. Received <Buffer ff d8 ff e1 10 c1 45 78 69 66 00 00 49 49 2a 00 08 00 00 00 0f 00 00 01 03 00 01 00 00 00 6c 05 00 00 01 01 03 00 01 00 ...
我已经尝试将path: body, 替换为path: new Uint8Array(body),,但没有成功。
知道我在这里做错了什么吗?
感谢您的帮助!
【问题讨论】:
-
很明显,该函数需要一个文件路径名,而不是缓冲区。
标签: javascript node.js server strapi