收藏
回答

HTTP API上传图片问题?

app.post("/uploadfile", upload.single("file"), (req, res) => {
	var file =req.file
	promise.then(data => {
		url = 'https://api.weixin.qq.com/tcb/uploadfile?access_token=' + JSON.parse(data).access_token
		data = {
			"env": 'laoqi-2gvugolvf7a76b77',
			"path": 'demo/'+  file.originalname
		}
		getData(url, data).then(dataa => {
			var res = dataa
			if (res.errmsg == 'ok') {
				
				requestData = {
					'key': 'demo/'+ file.originalname,
					'Signature': res['authorization'],
					'x-cos-security-token':  res['token'],
					'x-cos-meta-fileid': res['cos_file_id'],
					'file':{
						value:fs.createReadStream(file.path),
						options:{
							'filename': file.originalname,
							 'contentType': file.mimetype
						}
					}
				}
				getData(res['url'], requestData).then(data2 => {
					console.log(data2)
					// res.send(data2)
				}).catch(err => {
					console.log(err)
					res.send(err)
				})
			}
		}).catch(err => {
			console.log(err)
			res.send(err)
		})
	}).catch(data => {
		console.log(data);
	})

})

一直报错

<?xml version='1.0' encoding='utf-8' ?>
<Error>
        <Code>MethodNotAllowed</Code>
        <Message>The specified method is not allowed against this resource.</Message>
        <Resource>cos.ap-shanghai.myqcloud.com/6c61-laoqi-2gvugolvf7a76b77-1304583758/demo/QQ图片20200711091851.jpg</Res
ource>
        <RequestId>NWZlOTdkYzBfN2QzZjIyMDlfODQyYl8xY2RiY2Zj</RequestId>
        <TraceId>OGVmYzZiMmQzYjA2OWNhODk0NTRkMTBiOWVmMDAxODc0OWRkZjk0ZDM1NmI1M2E2MTRlY2MzZDhmNmI5MWI1OTczMmZiNDZmZjBmNTV
jMGU4NTViNDhhYWVjNzNkNzI4NWM1YTFlYWZiODhiYTgwMjZhMzcyMjBmNWVlZTMzNjI=</TraceId>
</Error>
回答关注问题邀请回答
收藏

1 个回答

  • MGee
    MGee
    2021-05-02

    能发帖求助的肯定找了很久问题,我是这个问题我觉得很隐蔽了,这个解决了就上传成功了,特意来回复下!contentType后要带上 boundary=---XXXXXX

    就是这样:

    contentType":"multipart/form-data; boundary=---------------------------7db1851cd1158",


    2021-05-02
    有用
    回复 1
    • 新微云科技
      新微云科技
      2022-03-13
      multipart/form-data; charset=utf-8; boundary=__X_PAW_BOUNDARY__"


      __X_PAW_BOUNDARY_ 这个值是什么?
      2022-03-13
      回复
登录 后发表内容
问题标签