vue結(jié)合egg的七牛云上傳圖片詳解

七牛云

參考網(wǎng)址:https://blog.csdn.net/Zhooson/article/details/78749205

1.在vue項目中

2.在egg router.js中寫router.get('/qiniu/token',controlle.qiniu.getToken);

3.在egg controller 中新建js文件并進行如下操作:

(1)引入七牛:var qiniu = require("qiniu");? 要先在后臺中下包(npm i qiniu --save)

(2) 引入path和fs: var path = require("path")

var fs=require('fs');

(3) var config=JSON.parse(fs.readFileSync(path.resolve(__dirname,"config.json")));

(4)在controller中新建config.json

(5) config.json中寫

{

? ? "AccessKey": "BqT4NGrPP045KPIzNFejv0kcK_EsApP81rxc8pYH",

? ? "SecretKey": "MpjGyi-1MT3xFwIFBVK31C8O9GJMf8YnSkC3iKJu",

? ? "Bucket": "muen",

? ? "UptokenUrl": "uptoken",

? ? "Domain": "pvwniq9y1.bkt.clouddn.com"

}

(6)在qiniu.js中寫密鑰對象:var mac = new qiniu.auth.digest.Mac(config.AccessKey, config.SecretKey);

(7) 在 async中 var token = putPolicy.uploadToken(mac);

(8)上傳的信息? ? scope(哪一個空間) returnBody(返回的信息)

key(要和域名拼接? 拼接好之后得到完整路徑)

var options = {

? ? scope: config.Bucket,

? ? returnBody:

? ? ? '{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}'

? };

(9) 上傳的配置對象

var putPolicy = new qiniu.rs.PutPolicy(options);

(10)? ctx.body={

? ? ? ? ? ? uptoken: token,

? ? ? ? ? ? domain: config.Domain

? ? ? ? }

(11)運行可拿到token(在小黑板中運行curl http://127.0.0.1:7001/qiniu/token)

(12)在頁面中寫

<el-upload

? ? ? :action="actionUrl"

? ? ? list-type="picture-card"

? ? ? :data="postData"

? ? ? :on-preview="handlePictureCardPreview"

? ? ? :on-remove="handleRemove"

? ? ? :auto-upload="true"

? ? ? :on-success="uploadS"

? ? ? :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>

其中action:上傳的地址? 七牛云:http(s)://upload.qiniup.com

data:請求體? postData:{token}

on-preview:預(yù)覽

on-remove:刪除

on-success:上傳成功

before-upload:上傳之前的操作? beforeImgUpload:返回false不往下執(zhí)行

附上上傳圖片的代碼:

<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 {get} from '../../service/request'

export default {

? props: {},

? components: {},

? data() {

? ? return {

? ? ? actionUrl:'https://upload.qiniup.com',

? ? ? postData:{},

? ? ? dialogImageUrl: "",

? ? ? dialogVisible: false

? ? };

? },

? computed: {},

? methods: {

? ? handleRemove(file, fileList) {

? ? ? console.log(file, fileList);

? ? ? // deletes("/qiniu/deleteFile?key=FnlW9-wyPRB2AQgyHlAz-9-Tdzl0").then((res)=>{

? ? ? //? console.log(res)

? ? ? //? if(res.code===1){

? ? ? //? ? this.$message.success('刪除成功')

? ? ? //? }else{

? ? ? //? ? this.$message.success('七牛云服務(wù)器刪除失敗')

? ? ? //? }

? ? ? // })

? ? },

? ? 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)

? ? },

? ? 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>


上圖是圖片名稱? 后面可以改圖片大小

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,229評論 0 3
  • 在管理后臺系統(tǒng)中,這次使用的是百度編輯器 首先在vue的目錄結(jié)構(gòu)static下面,把從網(wǎng)上下載的UE解壓放進去,如...
    Light_shallow閱讀 7,905評論 0 1
  • 一、上傳文件實現(xiàn) 兩種實現(xiàn)方式: 1、直接action <el-upload class="upload-fil...
    歲末Zzz閱讀 11,482評論 2 5
  • 參考:九宮格(圖片上傳) 實現(xiàn)oss簽名直傳 自定義圖標 最終樣式: 1.html代碼 <in...
    _小生不才_閱讀 1,684評論 0 0
  • 1.場景還原 近期,由于項目需求需要上傳圖片logo,筆者在探索的過程中遇到過很多坑,今晚就把個人經(jīng)驗分享出來盡量...
    老張家的獨苗閱讀 2,705評論 0 2

友情鏈接更多精彩內(nèi)容