uploadFile.js
1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import config from './config'
export default function (filePath,options) {
return new Promise(async (resolve, reject) => {
if (!filePath || filePath.length < 9) {
reject("文件错误");
}
// 获取上传的文件类型
let fileTypeIndex = filePath.lastIndexOf("/");
let fileType = filePath.substring(fileTypeIndex + 1);
let aliyunFileKey = config.folder + fileType;
const aliyunServerURL = config.uploadUrl; // OSS地址
const { signature, securityToken, lsaccessKeyId,policyBase64 } = options
wx.uploadFile({
url: aliyunServerURL, // 开发者服务器url
filePath: filePath, // 要上传文件资源的路径
name: "file", // 必须填file
formData: {
key: aliyunFileKey,
policy: policyBase64,
OSSAccessKeyId: lsaccessKeyId,
success_action_status: "200", // 让服务端返回200, 不然默认会返回204
signature: signature,
"x-oss-security-token": securityToken, // 使用STS签名时必传。
},
success: function (res) {
console.log(res, 444);
if (res.statusCode != 200) {
reject(
new Error(
"上传错误:" +
JSON.stringify({
statusCode: res.statusCode,
filePath,
key: aliyunFileKey,
policy: policyBase64,
OSSAccessKeyId: lsaccessKeyId,
signature: signature,
"x-oss-security-token": securityToken,
})
)
);
}
resolve(aliyunServerURL + aliyunFileKey);
},
fail: function (err) {
err.domainName = aliyunServerURL;
reject(err);
},
});
});
};