刪除文件:
參見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>