删除文件:
参见node.js.sdk文档
1.在egg router.js中router.delete('/qiniu/delete',controller.lmj.qiniu.deleteFile); //删除七牛云文件
2.在controller中的qiniu.js中
async deleteFile(){
const {ctx}= this;
let key=ctx.query.key;
var configManager = new qiniu.conf.Config();
configManager.zone = qiniu.zone.Zone_z0;
var bucketManager = new qiniu.rs.BucketManager(mac, configManager);
let result=await new Promise((resolve,reject)=>{
bucketManager.delete(config.Bucket, key, function(err, respBody, respInfo) {
if (err) {
console.log(err);
reject({
massage:'删除失败',
code:-1
})
} else {
console.log(respInfo.statusCode);
console.log(respBody);
resolve({
massage:'删除成功',
code:1
})
}
});
})
if(result.code===1){
ctx.status=200
}else{
ctx.status=522
}
ctx.body=result;
}
附上页面代码:
<template>
<div class="lmjboxcontain">
<el-upload
:action="actionUrl"
list-type="picture-card"
:data="postData"
:on-preview="handlePictureCardPreview"
:on-remove="handleRemove"
:auto-upload="true"
:on-success="uploadSuccess"
:on-error="uploadError"
:before-upload="beforeImgUpload"
>
<i class="el-icon-plus"></i>
</el-upload>
<el-dialog :visible.sync="dialogVisible">
<img width="100%" :src="dialogImageUrl" alt />
</el-dialog>
</div>
</template>
<script>
import axios from 'axios'
import {deletes} from '../../service/request'
export default {
props: {},
components: {},
data() {
return {
actionUrl:'https://upload.qiniup.com',
postData:{},
dialogImageUrl: "",
dialogVisible: false,
key:""
};
},
computed: {},
methods: {
handleRemove(file, fileList) {
console.log(file, fileList);
deletes('/api/qiniu/delete?key='+this.key).then((res)=>{
console.log(res)
})
},
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url;
this.dialogVisible = true;
},
beforeImgUpload(file){
const isJPEG=file.type==='image/jpeg';
const isLt2M=file.size/1024/1024<2;
if(!isJPEG){
this.$message.error('上传头像图片只能是JPEG格式')
}
if(!isLt2M){
this.$message.error('上传头像图片大小不能超过2MB')
}
return isJPEG&&isLt2M
},
uploadSuccess(response,file,fileList){
console.log(response)
this.key=response.key;
},
uploadError(err,file,fileList){
console.log(err)
}
},
created() {
axios.get('/api/qiniu/token').then((res)=>{
console.log(res)
this.postData={
token:res.data.uptoken
}
})
},
mounted() {}
};
</script>
<style scoped lang="">
</style>