1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| <template> <div class="container-area"> <el-upload ref="upload" class="upload-demo" action="https://jsonplaceholder.typicode.com/posts/" :on-preview="handlePreview" :on-remove="handleRemove" :on-change="handleChange" :file-list="fileList" :auto-upload="false" > <el-button slot="trigger" size="small" type="primary">选取文件</el-button> <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> <el-button slot="trigger" size="small" type="primary" @click="blobToBase64(file)">File转base64</el-button>
<p>{{ base64 }}</p> <el-button slot="trigger" size="small" type="primary" @click="base64ToFile()">base64转File</el-button> <p /> <el-button slot="trigger" size="small" type="primary" @click="base64ToBlob()">base64转Blob</el-button> <p>{{ blob }}</p> <el-button slot="trigger" size="small" type="primary" @click="blob2File(blob,'新文件')">Blob转File</el-button> <el-button slot="trigger" size="small" type="primary" @click="blob2ArrayBuffer(blob,'新文件')">Blob转ArrayBuffer</el-button> <el-button slot="trigger" size="small" type="primary" @click="downloadBlobUrl">Blob URL 用于文件上传下载</el-button> </div> </template>
<script> export default { data() { return { fileList: [], base64: '', file: null, blob: '' } }, methods: { submitUpload() { console.log(this.fileList) }, handleRemove(file, fileList) { console.log(file, fileList) }, handlePreview(file) { console.log(file) }, handleChange(file, fileList) { this.file = file.raw console.log(this.file) }, blobToBase64(blob) { const reader = new FileReader() reader.addEventListener('load', () => { console.log(reader.result) this.base64 = reader.result }) reader.readAsDataURL(blob) }, base64ImgtoFile(baseUrl, filename = '新建文本文档') { const arr = baseUrl.split(',') const mime = arr[0].match(/:(.*?);/)[1] const suffix = mime.split('/')[0] const bstr = atob(arr[1]) let n = bstr.length const u8arr = new Uint8Array(n) while (n--) { u8arr[n] = bstr.charCodeAt(n) } return new File([u8arr], `${filename}.${suffix}`, { type: mime }) }, dataURItoBlob(dataURI) { var byteString = atob(dataURI.split(',')[1]) var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0] var ab = new ArrayBuffer(byteString.length) var ia = new Uint8Array(ab) for (var i = 0; i < byteString.length; i++) { ia[i] = byteString.charCodeAt(i) } return new Blob([ab], { type: mimeString }) }, base64ToFile() { const file = this.base64ImgtoFile(this.base64) console.log(file) }, base64ToBlob() { const blob = this.dataURItoBlob(this.base64) this.blob = blob console.log(blob) },
blob2File(blob, fileName) { blob.lastModifiedDate = new Date() blob.name = fileName console.log(blob) return blob }, blob2ArrayBuffer() { const reader = new FileReader() reader.onload = function() { const content = reader.result console.log(content) } reader.readAsArrayBuffer(this.blob) }, printRes(res) { console.log(res) }, downloadBlobUrl() { var url = window.URL.createObjectURL(this.blob) console.log(url) const link = document.createElement('a') link.style.display = 'none' link.href = url link.setAttribute('download', 'helloworld.txt') document.body.appendChild(link) link.click() } } } </script>
<style lang="scss" scoped> .container-area{ } </style>
|