【发布时间】:2021-06-06 00:26:04
【问题描述】:
我正在使用 react antd 上传图片。这是我处理上传请求的特快:
router.post('/upload-image', upload.any(), async function(req, res, next) {
const file = req.files[0];
const form = new FormData();
form.append('image', file.buffer);
form.append('type', 'file');
const imgurInstance = new imgur({
refreshToken: user.imgurRefreshToken,
clientId: IMGUR_CLIENT_ID,
clientSecret: IMGUR_CLIENT_SECRET
});
await imgurInstance.getAccessToken();
const response = await imgurInstance.uploadImage(form);
const data = await response.json();
});
这里记录了 req.files[0] 控制台:
{
fieldname: 'file',
originalname: 'headscratch.jpeg',
encoding: '7bit',
mimetype: 'image/jpeg',
buffer: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 01 00 01 00 00 ff e1 00 42 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 01 87 69 00 04 00 00 00 01 00 00 ... 13803 more bytes>,
size: 13853
}
这是我使用 node-fetch 的 uploadImage 函数
uploadImage (form){
const path = `/3/upload`;
const headers = {
'Authorization': `Bearer ${this.accessToken}`,
...form.getHeaders()
};
const options = {
method: 'POST',
body: form,
headers
};
return fetch(`${this.BASE_API_URL}${path}`, options);
}
我总是从 imgur 获得 400 分,但他们没有提供任何详细信息。
{
status: 400,
success: false,
data: { error: 'Bad Request', request: '/3/upload', method: 'POST' }
}
我尝试过使用 base64,只是使用 form-data 上传一个 url,我仍然收到 400 错误。有人对如何成功进行此通话有任何指导吗?谢谢。
【问题讨论】:
标签: node.js multer form-data imgur