|
|
|
|
import OSS from "ali-oss"
|
|
import utils from './utils.js'
|
|
import config from '../config.js'
|
|
|
|
|
|
function uploadFileToOSS(file) {
|
|
|
|
uni.showLoading({
|
|
title: '上传中...'
|
|
});
|
|
|
|
return new Promise((resolve,reject) => {
|
|
// 创建OSS实例
|
|
const client = new OSS(config.aliOss.config);
|
|
|
|
// 设置文件名和文件目录
|
|
const suffix = '.' + file.name.split('.').pop();
|
|
const fileName = utils.generateUUID() + suffix; // 注意:文件名需要是唯一的
|
|
|
|
// 使用put接口上传文件
|
|
client.multipartUpload(fileName, file, {
|
|
headers: {
|
|
'Content-Disposition': 'inline',
|
|
'Content-Type': file.type
|
|
}
|
|
}).then(res => {
|
|
uni.hideLoading();
|
|
resolve(config.aliOss.url + res.name);
|
|
}).catch(err => {
|
|
uni.hideLoading();
|
|
reject(err)
|
|
})
|
|
})
|
|
}
|
|
|
|
|
|
export default uploadFileToOSS
|