|
@@ -1,52 +1,270 @@
|
|
|
<template>
|
|
|
- <div>
|
|
|
- <Upload type="drag" :action="uploadUrl" multiple :on-success="getFileUrl" :before-upload="beforeUpload" class="upload-wrap">
|
|
|
- <p style="margin:20px 0px; margin-top:100px;font-size:30px;">点击或者拖拽上传</p>
|
|
|
- <Icon type="ios-cloud-upload" size="80" style="margin-bottom:100px;" />
|
|
|
- </Upload>
|
|
|
+ <div>
|
|
|
+ <Upload type="drag" action="" multiple :before-upload="beforeUpload" class="upload-wrap">
|
|
|
+ <p style="margin:20px 0px; margin-top:80px;font-size:30px;">点击或者拖拽上传</p>
|
|
|
+ <Icon type="ios-cloud-upload" size="80" style="margin-bottom:80px;" />
|
|
|
+ </Upload>
|
|
|
+ <div class="upload-file-box">
|
|
|
+ <div class="upload-file-item" v-for="(item,index) in uploadedList">
|
|
|
+ <Icon type="ios-folder" />
|
|
|
+ <p class="upload-file-name">{{item.fileName}}</p>
|
|
|
+ <img width="25" style="float:right;" src="@/assets/loading/loading3.svg" v-if="item.status == 0"/>
|
|
|
+ <Icon type="md-checkmark" style="float:right;" color="aqua" size="20" v-else-if="item.status == 1"/>
|
|
|
+ <Icon type="md-close" style="float:right;" color="red" size="20" v-else-if="item.status == 2"/>
|
|
|
+ </div>
|
|
|
</div>
|
|
|
+ </div>
|
|
|
</template>
|
|
|
<script>
|
|
|
- export default {
|
|
|
- data() {
|
|
|
- return {
|
|
|
+ require('@/utils/azure-storage-blob.js')
|
|
|
+ import sha1 from 'js-sha1'
|
|
|
+ export default {
|
|
|
+ data() {
|
|
|
+ return {
|
|
|
+ containerURL: undefined,
|
|
|
+ sasString: '',
|
|
|
+ uploadedList: [],
|
|
|
+ contentTypes: [
|
|
|
+ ['JPG', 'JPEG', 'PNG', 'GIF'],
|
|
|
+ ['AVI', 'MP4'],
|
|
|
+ ['PPT', 'PPTX', 'DOC', 'DOCX', 'PDF', 'XLS', 'XLSX', 'HTE', 'HETX']
|
|
|
+ ],
|
|
|
+ }
|
|
|
+ },
|
|
|
+ props: {
|
|
|
+ accountName: {
|
|
|
+ default: 'teammodelostest',
|
|
|
+ type: String
|
|
|
+ },
|
|
|
+ containerName: {
|
|
|
+ default: 'teammodelos',
|
|
|
+ type: String
|
|
|
+ },
|
|
|
+ pathName: {
|
|
|
+ default: '',
|
|
|
+ type: String
|
|
|
+ },
|
|
|
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ checkSize() {
|
|
|
+
|
|
|
+ },
|
|
|
+ initBlob() {
|
|
|
+ this.containerURL = new window.azblob.ContainerURL(
|
|
|
+ `https://${this.accountName}.blob.core.chinacloudapi.cn/${this.containerName + this.pathName}?${this.sasString}`,
|
|
|
+ window.azblob.StorageURL.newPipeline(new window.azblob.AnonymousCredential())
|
|
|
+ )
|
|
|
+ },
|
|
|
+ dataURLtoFile(dataurl, filename) {
|
|
|
+ var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
|
|
|
+ bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
|
|
|
+ while (n--) {
|
|
|
+ u8arr[n] = bstr.charCodeAt(n);
|
|
|
+ }
|
|
|
+ return new File([u8arr], filename, { type: mime });
|
|
|
+ },
|
|
|
+ compressFile(file,fileInfo) {
|
|
|
+ let reader = new FileReader()
|
|
|
+ reader.readAsDataURL(file)
|
|
|
+ reader.onload = (e) => {
|
|
|
+ let dataUrl = e.target.result
|
|
|
+ let img = new Image()
|
|
|
+ img.src = dataUrl
|
|
|
+ img.onload = () => {
|
|
|
+ let canvas = document.createElement('canvas')
|
|
|
+ let ctx = canvas.getContext('2d')
|
|
|
+ ctx.drawImage(img, 0, 0, canvas.width, canvas.height)
|
|
|
+ let newImgData = canvas.toDataURL(file.type, 0.2)
|
|
|
+ var resultFile = this.dataURLtoFile(newImgData, file.name)
|
|
|
+ this.initBlob()
|
|
|
+ const compressBlobURL = window.azblob.BlockBlobURL.fromContainerURL(
|
|
|
+ this.containerURL,
|
|
|
+ 'compress' + file.name
|
|
|
+ )
|
|
|
+ window.azblob.uploadBrowserDataToBlockBlob(
|
|
|
+ window.azblob.Aborter.none,
|
|
|
+ resultFile,
|
|
|
+ compressBlobURL
|
|
|
+ ).then(
|
|
|
+ res => {
|
|
|
+ fileInfo['compressUrl'] = `https://${this.accountName}.blob.core.chinacloudapi.cn/${this.containerName + this.pathName}/compress${file.name}`
|
|
|
+ console.log(fileInfo)
|
|
|
+ this.$emit("successData", fileInfo)
|
|
|
+ },
|
|
|
+ err => {
|
|
|
+ fileInfo['status'] = 2
|
|
|
+ console.log(err)
|
|
|
+ }
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ uploadToBlob(file) {
|
|
|
+ const blockBlobURL = window.azblob.BlockBlobURL.fromContainerURL(
|
|
|
+ this.containerURL,
|
|
|
+ file.name
|
|
|
+ )
|
|
|
+ let fileItem = {}
|
|
|
+ fileItem['fileName'] = file.name
|
|
|
+ fileItem['blobUrl'] = `https://${this.accountName}.blob.core.chinacloudapi.cn/${this.containerName + this.pathName}/${file.name}`
|
|
|
+ fileItem['extension'] = file.name.substring(file.name.lastIndexOf('.') + 1)
|
|
|
+ fileItem['size'] = file.size
|
|
|
+ fileItem['status'] = 0
|
|
|
+ fileItem['contentType'] = file.type
|
|
|
+ let type = 'other';
|
|
|
+ for (let item in this.contentTypes) {
|
|
|
+ if (this.contentTypes[item].indexOf(fileItem.extension.toUpperCase()) !== -1) {
|
|
|
+ switch (true) {
|
|
|
+ case item == 0:
|
|
|
+ type = 'picture'
|
|
|
+ break
|
|
|
+ case item == 1:
|
|
|
+ type = 'video'
|
|
|
+ break
|
|
|
+ case item == 2:
|
|
|
+ type = 'document'
|
|
|
+ break
|
|
|
+ default:
|
|
|
+ break
|
|
|
}
|
|
|
- },
|
|
|
- props: {
|
|
|
- uploadUrl: {
|
|
|
- default: '',
|
|
|
- type: String
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fileItem['type'] = type
|
|
|
+ this.uploadedList.push(fileItem)
|
|
|
+ window.azblob.uploadBrowserDataToBlockBlob(
|
|
|
+ window.azblob.Aborter.none,
|
|
|
+ file,
|
|
|
+ blockBlobURL
|
|
|
+ ).then(
|
|
|
+ res => {
|
|
|
+ let reader = new FileReader()
|
|
|
+ reader.readAsArrayBuffer(file)
|
|
|
+ //reader.readAsArrayBuffer(file)
|
|
|
+ reader.onload = (ev) => {
|
|
|
+ try {
|
|
|
+ let fileRes = ev.target.result
|
|
|
+ let str = sha1(fileRes)
|
|
|
+ console.log(str)
|
|
|
+ let index = this.getIndex(this.uploadedList, fileItem)
|
|
|
+ this.uploadedList[index].status = 1
|
|
|
+ this.uploadedList[index]['createTime'] = Date.parse(res.lastModified)
|
|
|
+ this.uploadedList[index]['sha1Code'] = str
|
|
|
+ if (this.uploadedList[index].type == 'picture' || this.uploadedList[index].type == 'video') {
|
|
|
+ console.log(this.uploadedList[index])
|
|
|
+ this.compressFile(file,this.uploadedList[index])
|
|
|
+ } else {
|
|
|
+ this.$emit("successData", this.uploadedList[index])
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ console.log(e)
|
|
|
+ }
|
|
|
}
|
|
|
- },
|
|
|
- methods: {
|
|
|
- checkSize() {
|
|
|
-
|
|
|
- },
|
|
|
- getFileUrl() {
|
|
|
-
|
|
|
- },
|
|
|
- beforeUpload(file) {
|
|
|
- this.$api.uploadFile.getBlobSAS().then(
|
|
|
- (res) => {
|
|
|
-
|
|
|
- },
|
|
|
- (err) => {
|
|
|
-
|
|
|
- }
|
|
|
- )
|
|
|
+
|
|
|
+ },
|
|
|
+ err => {
|
|
|
+ fileItem['status'] = 2
|
|
|
+ console.log(err)
|
|
|
+ }
|
|
|
+ )
|
|
|
+ return false
|
|
|
+ },
|
|
|
+ beforeUpload(file) {
|
|
|
+ console.log(file)
|
|
|
+ let url = file.name
|
|
|
+ this.$api.uploadFile.getContainerSAS().then(
|
|
|
+ (res) => {
|
|
|
+ if (res.error == null) {
|
|
|
+ this.sasString = res.result.data
|
|
|
+ this.initBlob()
|
|
|
+ this.uploadToBlob(file)
|
|
|
+ } else {
|
|
|
+ alert("API error!")
|
|
|
}
|
|
|
- },
|
|
|
- mounted() {
|
|
|
-
|
|
|
- },
|
|
|
- created() {
|
|
|
-
|
|
|
+ },
|
|
|
+ (err) => {
|
|
|
+ alert("API error!")
|
|
|
+ }
|
|
|
+ )
|
|
|
+ return false
|
|
|
+ },
|
|
|
+ canvasDataURL(path, obj) {
|
|
|
+ var img = new Image()
|
|
|
+ img.src = path
|
|
|
+ img.onload = function () {
|
|
|
+ var that = this
|
|
|
+ // 默认按比例压缩
|
|
|
+ var w = that.width,
|
|
|
+ h = that.height,
|
|
|
+ scale = w / h
|
|
|
+ w = obj.width || w
|
|
|
+ h = obj.height || (w / scale)
|
|
|
+ var quality = 0.7 // 默认图片质量为0.7
|
|
|
+ //生成canvas
|
|
|
+ var canvas = document.createElement('canvas')
|
|
|
+ var ctx = canvas.getContext('2d')
|
|
|
+ // 创建属性节点
|
|
|
+ var anw = document.createAttribute("width")
|
|
|
+ anw.nodeValue = w
|
|
|
+ var anh = document.createAttribute("height")
|
|
|
+ anh.nodeValue = h
|
|
|
+ canvas.setAttributeNode(anw)
|
|
|
+ canvas.setAttributeNode(anh)
|
|
|
+ ctx.drawImage(that, 0, 0, w, h)
|
|
|
+ // 图像质量
|
|
|
+ if (obj.quality && obj.quality <= 1 && obj.quality > 0) {
|
|
|
+ quality = obj.quality
|
|
|
+ }
|
|
|
+ // quality值越小,所绘制出的图像越模糊
|
|
|
+ var base64 = canvas.toDataURL('image/jpeg', quality)
|
|
|
+ // 回调函数返回base64的值
|
|
|
+ return base64
|
|
|
}
|
|
|
- }
|
|
|
+ },
|
|
|
+ getIndex(_arr, _obj) {
|
|
|
+ var len = _arr.length;
|
|
|
+ for (let i = 0; i < len; i++) {
|
|
|
+ if (this.isObjEqual(_arr[i],_obj)) {
|
|
|
+ return parseInt(i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
+ },
|
|
|
+ isObjEqual(o1, o2) {
|
|
|
+ var props1 = Object.keys(o1);
|
|
|
+ var props2 = Object.keys(o2);
|
|
|
+ console.log(props1);
|
|
|
+ if (props1.length != props2.length) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ for (var i = 0, max = props1.length; i < max; i++) {
|
|
|
+ var propName = props1[i];
|
|
|
+ if (o1[propName] !== o2[propName]) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ },
|
|
|
+ mounted() {
|
|
|
+ this.uploadedList = []
|
|
|
+ },
|
|
|
+ created() {}
|
|
|
+ }
|
|
|
|
|
|
</script>
|
|
|
<style scoped>
|
|
|
- .upload-wrap {
|
|
|
+ .upload-file-item {
|
|
|
+ cursor:pointer;
|
|
|
+ color:#909090;
|
|
|
+ margin-top:5px;
|
|
|
+ }
|
|
|
+ .upload-file-item:hover {
|
|
|
+ color:white;
|
|
|
}
|
|
|
+ .upload-file-name {
|
|
|
+ display:inline-block;
|
|
|
+ margin-left:10px;
|
|
|
+ }
|
|
|
</style>
|