vue 使用OSS上傳圖片或附件講解
vue項(xiàng)目中使用OSS上傳圖片或附件
上傳圖片和附件這里不做區(qū)別;上傳的流程都一樣;
1、新建oss.js文件,封裝使用oss (需要安裝包ali-oss)
const OSS = require('ali-oss') const OSSConfig = { uploadHost: 'http://xxxxx.oss-cn-shenzhen.aliyuncs.com', //OSS上傳地址 ossParams: { region: 'oss-cn-shenzhen', success_action_status: '200', // 默認(rèn)200 accessKeyId: 'LTxxxxxxxxxxxxxxxvnkD', accessKeySecret: 'J6xxxxxxxxxxxxxxxxiuv', bucket: 'xxxxxxxx-czx', }, } function random_string(len) { len = len || 32 var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678' var maxPos = chars.length var pwd = '' for (let i = 0; i < len; i++) { pwd += chars.charAt(Math.floor(Math.random() * maxPos)) } return pwd } function uploadOSS(file) { return new Promise(async (resolve, reject) => { const fileName = `${random_string(6)}_${file.name}` let client = new OSS({ region: OSSConfig.ossParams.region, accessKeyId: OSSConfig.ossParams.accessKeyId, accessKeySecret: OSSConfig.ossParams.accessKeySecret, bucket: OSSConfig.ossParams.bucket, }) const res = await client.multipartUpload(fileName, file) // resolve(res) // 或者返回如下: resolve({ fileUrl: `${OSSConfig.uploadHost}/${fileName}`, fileName: file.name }) }) } export { uploadOSS }
2、頁(yè)面中使用oss.js
這里是對(duì)elementUI的上傳組件二次封裝,里面不使用組件的action上傳圖片和附件,使用oss上傳方式;
<template> <div class="upload-file"> <el-upload ref="fileUpload" action="" :headers="uploadProps.headers" list-type="picture-card" :show-file-list="false" :http-request="fnUploadRequest" :on-success="handleSuccess" :on-error="handleError" :before-upload="handleUpload" > <slot></slot> </el-upload> </div> </template> <script> import { getAccessToken, getRefreshToken, getAccessTokenTTL } from "@/utils/auth"; import { uploadOSS } from '@/utils/oss'; export default { name: "index", data() { return {}; }, computed: { userAccountID() { return this.$store.state.user.userAccountID; }, uploadProps() { return { // action: `${process.env.VUE_APP_BASE_API}/api/file/upload`, headers: { // 接口可能要帶token: "", Authorization: getAccessToken(), }, data: {}, }; }, }, methods: { handleExceed(file, fileList){ // console.log(file, fileList); this.$message.error('上傳失敗,限制上傳數(shù)量10個(gè)文件以內(nèi)!'); }, handleUpload(file, fileList){ // console.log(file, fileList); var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1) const extension = testmsg === 'xlsx' || testmsg === 'xls' || testmsg === 'docx' || testmsg === 'doc' || testmsg === 'pdf' || testmsg === 'zip' || testmsg === 'rar' || testmsg === 'ppt' || testmsg === 'txt' const isLimit10M = file.size / 1024 / 1024 < 10 var bool = false; if(extension && isLimit10M){ bool = true; } else { bool = false; } if(!extension) { this.$message.error('請(qǐng)選擇附件文件類(lèi)型!'); return bool; } if(!isLimit10M) { this.$message.error('上傳失敗,不能超過(guò)10M!'); return bool; } return bool; }, handleSuccess(res) { // console.log(res); if (res) { this.$emit('fileData', res) this.$message.success("上傳附件成功!"); } }, handleError(err){ this.$message.error('上傳附件失??!'); }, // 上傳圖片 async fnUploadRequest(options) { try { // console.log(options); let file = options.file; // 拿到 file let res = await uploadOSS(file) console.log(res); // 返回?cái)?shù)據(jù) this.$emit("fileData", res); this.$message.success("上傳附件成功!"); } catch (e) { this.$message.error('上傳附件失??!'); } }, }, }; </script> <style lang="scss" scoped> ::v-deep .el-upload, .el-upload--picture-card { // width: 50px; height: 0px; border: none; line-height: 0; display: block; background: #f5f6fb; } .el-upload { width: 50px; } .img-cont { width: 50px; height: 24px; background: #f5f6fb; .img-icon { color: #ccc; } .img-text { font-size: 12px; height: 24px; color: #000; } } </style>
使用封裝的上傳組件
<div class="task-detail pr"> <el-form-item label="報(bào)備原因" prop="taskDesc" required> <div class="flex-center upload-position"> <ImgUpload @imgData="imgData" /> <FileUpload class="ml10" @fileData="fileData" /> </div> <el-input type="textarea" v-model="ruleForm.taskDesc" placeholder="請(qǐng)輸入報(bào)備原因" maxlength="200" @input="checkiptTaskDesc()" ></el-input> <div class="ipt-taskDesc-length">{{checkIptTaskDescLen}}/200</div> <div class="img-list mt10 flex"> <ImgZoomIn :imagesList="imagesList" @deleteImg="deleteImg"></ImgZoomIn> </div> <div class="dotted-line" v-if="imagesList.length > 0 && filesList.length > 0"></div> <div class="file-item"> <HandleFile :filesList="filesList" @deleteFile="deleteFile"></HandleFile> </div> </el-form-item> </div>
效果如下
到此這篇關(guān)于vue 使用OSS上傳圖片或附件講解的文章就介紹到這了,更多相關(guān)vue 使用OSS上傳圖片或附件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談實(shí)現(xiàn)vue2.0響應(yīng)式的基本思路
這篇文章主要介紹了淺談實(shí)現(xiàn)vue2.0響應(yīng)式的基本思路,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02vue-simple-uploader上傳成功之后的response獲取代碼
這篇文章主要介紹了vue-simple-uploader上傳成功之后的response獲取代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧~2020-09-09vue ElementUI實(shí)現(xiàn)異步加載樹(shù)
這篇文章主要為大家詳細(xì)介紹了vue ElementUI實(shí)現(xiàn)異步加載樹(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06Vue組件實(shí)現(xiàn)數(shù)字滾動(dòng)抽獎(jiǎng)效果
這篇文章主要為大家詳細(xì)介紹了Vue組件實(shí)現(xiàn)數(shù)字滾動(dòng)抽獎(jiǎng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03