获取文件上传链接 uploadFil,获取到返回数据后,拼装POST请求(报400)
https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/uploadFile.html 我实在是找不出问题出在哪里了?可以跟我说我是哪里有误了? [图片] 需要你替换 (你的appid 你的secret 你的云环境ID 你的图片上传路径)
再设置浏览器可以跨域就可以运行了
(async function() {
const appid = '你的appid'
const secret = '你的secret'
const env = '你的云环境ID'
const path = '你的上传路径'
function getToken() {
return new Promise((resolve, reject) => {
$.ajax({
url: `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${secret}`,
type: 'get',
success: function(res) {
resolve(res.access_token)
}
})
})
}
function getUrl() {
return new Promise((resolve, reject) => {
$.ajax({
url: 'https://api.weixin.qq.com/tcb/uploadfile?access_token=' + access_token,
/*接口域名地址*/
type: 'post',
contentType: "application/json", //必须有
dataType: "json", //表示返回值类型,不必须
data: JSON.stringify({
"env": env,
"path": path
}),
success: function(res) {
resolve(res);
}
})
})
}
const access_token = await getToken()
const data = await getUrl()
$('#upload').on('click', function() {
console.log('$("#photo")[0].files[0]', $("#photo")[0].files[0])
var formData = new FormData();
formData.append("file", $("#photo")[0].files[0]);
formData.append("key", path);
formData.append("Signature", data.authorization);
formData.append("x-cos-security-token", data.token);
formData.append("x-cos-meta-fileid", data.cos_file_id);
console.log('formData', formData)
$.ajax({
url: data.url,
/*接口域名地址*/
type: 'post',
// headers: {
// contentType: "multipart/form-data"
// },
// 'Content-Type': "multipart/form-data",
data: formData,
contentType: false,
processData: false,
success: function(res) {
resolve(res);
}
})
})
})()