共有代碼
function getTimestamp() {
return new Date().getTime() / 1000;
}
dd.chooseImage({
sourceType: ['camera', 'album'],
count: 3,
success: (res) => {
(res.filePaths || res.apFilePaths || []).forEach(path => {
uploadFile(path);
});
},
fail: (res) => {
if (res.error != 11) {
dd.showToast({content: 'fail'});
}
}
});
function uploadFile(path) {
if (!path) return;
const hashCode = hash
.sha1()
.update(`${getTimestamp() + Math.random()}`)
.digest('hex');
const fileName = `${hashCode}${/\.[^\.]+$/.exec(filePath)[0]}`;
const name = filePath.match(/\/([^/]*)$/)[1];
const key = `attachments/${moment().format('YYYYMMDD')}/${fileName}`;
const { fileToken: { accessKeyId, policy, securityToken, signature, expire } } = app.globalData;
const formData = {
name, key, policy,
OSSAccessKeyId: accessKeyId,
Signature: signature,
'x-oss-security-token': securityToken,
success_action_status: '200',
};
dd.uploadFile({
url: config.fileHost, // 替換自己的阿里云地址
fileType: 'image',
fileName: 'file',
filePath: filePath,
formData: formData,
success: (res) => {
console.log(res, { key: formData.key, name: name });
},
fail: (err) => {
dd.showToast({ content: '上傳失敗采转,稍后再試耙箍!' });
},
complete: (res) => {
dd.hideLoading();
}
});
}
function uploadFile(path) {
if (!path) return;
const hashCode = hash
.sha1()
.update(`${getTimestamp() + Math.random()}`)
.digest('hex');
const fileName = `${hashCode}${/\.[^\.]+$/.exec(filePath)[0]}`;
const key = `attachments/${moment().format('YYYYMMDD')}/${fileName}`;
const { token } = app.globalData;
const formData = {
token, key
};
dd.uploadFile({
url: 'https://upload-z2.qiniup.com',
fileType: 'image',
fileName: 'file',
filePath: filePath,
formData: formData,
success: (res) => {
console.log(res.data);
},
fail: (err) => {
dd.showToast({
type: 'fail',
content: '上傳失敗,稍后再試辩昆!'
});
},
complete:
(res) => {
dd.hideLoading();
}
});
}