亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

vue+element+oss實現前端分片上傳和斷點續傳

瀏覽:4日期:2022-09-30 18:48:38

純前端實現: 切片上傳 斷點續傳 。斷點續傳需要在切上上傳的基礎上實現

前端之前上傳OSS,無需后端提供接口。先上完整代碼,直接復制,將new OSS里的參數修改成自己公司OSS相關信息后可用,如遇問題,請繼續往下看。

oss官方文檔

https://help.aliyun.com/document_detail/111268.html?spm=a2c4g.11186623.6.1111.5a583a07LknRUO

代碼允許所需環境:vue + element + ali-oss

安裝ali-oss: cnpm install ali-oss

代碼實現

<template> <div class='dashboard-editor-container'> <el-upload action='' ref='upload' :file-list='fileList' :limit='2' :on-change='handleChange' :on-remove='handleRemove' :auto-upload='false' accept='' > <el-button slot='trigger' size='small' type='primary'>選取文件</el-button> <el-button size='small' type='success' @click='submitForm'>上傳到服務器</el-button> <el-button size='small' type='success' @click='resumeUpload'>繼續</el-button> <el-button size='small' type='success' @click='stopUplosd'>暫停</el-button> <el-button size='small' type='success' @click='abortMultipartUpload'>清除切片</el-button> </el-upload> <el-progress :percentage='percentage' :status='uploadStatus'></el-progress> </div></template><script> let OSS = require(’ali-oss’) // 引入ali-oss插件 const client = new OSS({ region: ’oss-cn-shanghai’,//根據那你的Bucket地點來填寫 accessKeyId: ’LTA*********RaXY’,//自己賬戶的accessKeyId accessKeySecret: ’uu1************GiS’,//自己賬戶的accessKeySecret bucket: ’a******o’,//bucket名字 });export default { data () { return { fileList:[], file: null, tempCheckpoint: null, // 用來緩存當前切片內容 uploadId: ’’, uploadStatus: null, // 進度條上傳狀態 percentage: 0, // 進度條百分比 uploadName: ’’, //Object所在Bucket的完整路徑 } }, mounted() { // window.addEventListener(’online’, this.resumeUpload); }, methods: { // 點擊上傳至服務器 submitForm(file) { this.multipartUpload(); }, // 取消分片上傳事件 async abortMultipartUpload() { window.removeEventListener(’online’, this.resumeUpload) const name = this.uploadName; // Object所在Bucket的完整路徑。 const uploadId = this.upload; // 分片上傳uploadId。 const result = await client.abortMultipartUpload(name, uploadId); console.log(result, ’=======清除切片====’); }, // 暫停分片上傳。 stopUplosd () { window.removeEventListener(’online’, this.resumeUpload) // 暫停時清除時間監聽 let result = client.cancel(); console.log( result, ’---------暫停上傳-----------’) }, // 切片上傳 async multipartUpload () { if (!this.file) { this.$message.error(’請選擇文件’) return } this.uploadStatus = null // console.log('this.uploadStatus',this.file, this.uploadStatus); this.percentage = 0 try { //object-name可以自定義為文件名(例如file.txt)或目錄(例如abc/test/file.txt)的形式,實現將文件上傳至當前Bucket或Bucket下的指定目錄。 let result = await client.multipartUpload(this.file.name, this.file, { headers: { ’Content-Disposition’: ’inline’, ’Content-Type’: this.file.type //注意:根據圖片或者文件的后綴來設置,我試驗用的‘.png’的圖片,具體為什么下文解釋 }, progress: (p, checkpoint) => { this.tempCheckpoint = checkpoint; this.upload = checkpoint.uploadId this.uploadName = checkpoint.name this.percentage = p * 100 // console.log(p, checkpoint, this.percentage, ’---------uploadId-----------’) // 斷點記錄點。瀏覽器重啟后無法直接繼續上傳,您需要手動觸發上傳操作。 }, meta: { year: 2020, people: ’dev’ }, mime: this.file.type }); console.log(result, this.percentage, ’result= 切片上傳完畢=’); } catch (e) { window.addEventListener(’online’, this.resumeUpload) // 該監聽放在斷網的異常處理 // 捕獲超時異常。 if (e.code === ’ConnectionTimeoutError’) { // 請求超時異常處理 this.uploadStatus = ’exception’ console.log('TimeoutError'); // do ConnectionTimeoutError operation } // console.log(e) } }, // 恢復上傳。 async resumeUpload () { window.removeEventListener(’online’, this.resumeUpload) if (!this.tempCheckpoint) { this.$message.error(’請先上傳’) return } this.uploadStatus = null try { let result = await client.multipartUpload(this.file.name, this.file, { headers: { ’Content-Disposition’: ’inline’, ’Content-Type’: this.file.type //注意:根據圖片或者文件的后綴來設置,我試驗用的‘.png’的圖片,具體為什么下文解釋 }, progress: (p, checkpoint) => { this.percentage = p * 100 console.log(p, checkpoint, ’checkpoint----恢復上傳的切片信息-------’) this.tempCheckpoint = checkpoint; }, checkpoint: this.tempCheckpoint, meta: { year: 2020, people: ’dev’ }, mime: this.file.type }) console.log(result, ’result-=-=-恢復上傳完畢’) } catch (e) { console.log(e, ’e-=-=-’); } }, // 選擇文件發生改變 handleChange(file, fileList) { this.fileList = fileList.filter(row => row.uid == file.uid) this.file = file.raw // 文件改變時上傳 // this.submitForm(file) }, handleRemove(file, fileList) { this.percentage = 0 //進度條置空 this.fileList = [] }, }}</script><style scoped></style>

如果相關依賴已經安裝完畢,但是上述代碼操作時仍有報錯,請檢查以下問題

const client = new OSS({ region: ’oss-cn-shanghai’,//根據那你的Bucket地點來填寫 accessKeyId: ’LT******XY’,//自己賬戶的accessKeyId accessKeySecret: ’uu*********GiS’,//自己賬戶的accessKeySecret bucket: ’a******io’,//bucket名字 });

上述信息放在前端會存在安全問題,如在項目中使用盡量由后端接口提供。或使用STS臨時授權。demo中沒有,請自行探索。https://www.alibabacloud.com/help/zh/doc-detail/100624.htm?spm=a2c63.p38356.879954.5.7a234d04IQpf5I#concept-xzh-nzk-2gb

配置項中信息可以問后端或者運維,bucket的名字必須是你OSS上存在的且你有權限訪問的,不然會一直報 Pleasr create a busket first或者一直報跨域

當遇到跨域時,或者遇到報報錯信息中有etag時,請檢查OSS配置,然后找有OSS服務器權限人員進行配置:

vue+element+oss實現前端分片上傳和斷點續傳

window.addEventListener(’online’, this.resumeUpload)用于監聽網絡狀態(斷網狀態和連網狀態),實現斷網后恢復網絡自動上傳就必須設置監聽。

window.removeEventListener(’online’, this.resumeUpload)取消監聽。如果不設置取消監聽,聯網狀態下會一直處于進行上傳,因為一直滿足監聽條件`

headers: { ’Content-Disposition’: ’inline’, ’Content-Type’: this.file.type //注意:根據圖片或者文件的后綴來設置,我取得是文件的type,具體為什么下文解釋 },

’Content-Type’: this.file.type`的作用:加了在文件上傳完畢后,訪問文件鏈接時可以直接查看,否則會直接下載。

文件上傳完畢后查看,可以去resule.res.requestUrls中去取,但是注意要去點地址后面的 ?uploadId=******

上述代碼只是demo,代碼以實現功能為主,并不嚴謹,請自行完善。

到此這篇關于vue+element+oss實現前端分片上傳和斷點續傳的文章就介紹到這了,更多相關vue 分片上傳和斷點續傳內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Vue
相關文章:
主站蜘蛛池模板: 丰满美女福利视频在线播放 | 亚洲欧美日韩国产综合久 | 精品无人区一区二区三区a 精品无码一区在线观看 | 亚洲精品久久玖玖玖玖 | 欧美综合亚洲图片综合区 | 成人亚洲国产综合精品91 | 99久久中文字幕 | 视色4se成人午夜精品 | 人妖xxxx| 国产免费叼嘿在线观看 | 欧美一级毛片无遮 | 欧美黄色免费在线观看 | 亚洲欧美成人一区二区在线电影 | 黄色特级毛片 | 欧美一二三区在线 | 久久精视频| 国产成人综合精品 | 色天天| 午夜精品一区二区三区在线观看 | 国产精品久久天天影视 | 日本高清黄色 | a级毛片免费全部播放 | 黄色成人在线播放 | 国模午夜写真福利视频在线 | 99久久精品免费看国产免费 | 在线观看搡女人 | 久久精品免看国产 | 日本一级特黄啪啪片 | 国产在线观看麻豆91精品免费 | 亚州午夜 | 制服丝袜视频在线 | 97久久久久 | 爱啪网站| 亚洲狠狠狠一区二区三区 | 一级毛片真人免费播放视频 | 国产夜色福利院在线观看免费 | www.涩| 欧美成人午夜不卡在线视频 | 欧美视频日韩视频 | 亚洲欧美综合国产不卡 | 国产精选在线 |