• vue实现自定义上传下载


    前言: 对于文件图片的上传下载。首先要了解原理 - 前端所使用的二进制_哆来A梦没有口袋的博客-CSDN博客_前端上传文件二进制

    elementui自带了文件上传,图片上传,但是很多时候样式不满足,需要自己变更,那自定义实现上传下载又是如何的呢?

    原生的上传下载依赖的是什么?input输入框

    1.input

    input的type有很多种类型: text, password,button,radio,checkbox....

    上传主要用到的是 - file类

    <input type="file" accept multiple></input>

     accpt:

    属性规定了可通过文件上传提交的服务器接受的文件类型。

    常用的MIME类型

    1. 后缀名 MIME名称
    2. *.3gpp audio/3gpp, video/3gpp
    3. *.ac3 audio/ac3
    4. *.asf allpication/vnd.ms-asf
    5. *.au audio/basic
    6. *.css text/css
    7. *.csv text/csv
    8. *.doc application/msword
    9. *.dot application/msword
    10. *.dtd application/xml-dtd
    11. *.dwg image/vnd.dwg
    12. *.dxf image/vnd.dxf
    13. *.gif image/gif
    14. *.htm text/html
    15. *.html text/html
    16. *.jp2 image/jp2
    17. *.jpe image/jpeg
    18. *.jpeg image/jpeg
    19. *.jpg image/jpeg
    20. *.js text/javascript, application/javascript
    21. *.json application/json
    22. *.mp2 audio/mpeg, video/mpeg
    23. *.mp3 audio/mpeg
    24. *.mp4 audio/mp4, video/mp4
    25. *.mpeg video/mpeg
    26. *.mpg video/mpeg
    27. *.mpp application/vnd.ms-project
    28. *.ogg application/ogg, audio/ogg
    29. *.pdf application/pdf
    30. *.png image/png
    31. *.pot application/vnd.ms-powerpoint
    32. *.pps application/vnd.ms-powerpoint
    33. *.ppt application/vnd.ms-powerpoint
    34. *.rtf application/rtf, text/rtf
    35. *.svf image/vnd.svf
    36. *.tif image/tiff
    37. *.tiff image/tiff
    38. *.txt text/plain
    39. *.wdb application/vnd.ms-works
    40. *.wps application/vnd.ms-works
    41. *.xhtml application/xhtml+xml
    42. *.xlc application/vnd.ms-excel
    43. *.xlm application/vnd.ms-excel
    44. *.xls application/vnd.ms-excel
    45. *.xlt application/vnd.ms-excel
    46. *.xlw application/vnd.ms-excel
    47. *.xml text/xml, application/xml
    48. *.zip aplication/zip
    49. *.xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

    例如:限制只能上传图片

     <input type="file" class="file"  accept="image/x-png, image/jpg, image/jpeg"/>

    multiple - 定义是否多选

    2.在vue中应用

    在vue中灵活应用input, 写好上传样式,触发input回调实现上传

    例如:上传头像

     html部分

    1. <el-form-item label="上传头像">
    2. <div class="upload-img">
    3. <ul>
    4. <li @click="upload"><i class="iconfont icon-avar"></i> <span>上传</span></li>
    5. <li><img :src="src ? src : require('../../assets/imgs/avar.png')"/> <div><div class="op"></div><div class="fonts"><i class="iconfont icon-big"></i><span>预览图</span></div></div></li>
    6. </ul>
    7. <p>建议上传图片尺寸为60*60px,若不设置则使用系统默认头像</p>
    8. <input type="file" class="file" ref="upload" accept="image/x-png, image/jpg, image/jpeg" @change="submitUpload"/>
    9. </div>
    10. </el-form-item>

    css

    留下input框的dom结构,但是不占用位置

    1. .file {
    2. display: block;
    3. width: 0;
    4. height: 0;
    5. }

    js

    点击upload触发input框上传,将选取文件上传给后端

    1. upload() {
    2. this.$nextTick(() => {
    3. this.$refs.upload.click()
    4. })
    5. },
    6. submitUpload(e) {
    7. const files = e.target.files
    8. const rawFile = files[0] // only use files[0]
    9. if (!rawFile) return
    10. if(this.loading) return
    11. this.loading = true
    12. this.$util.uploadFile({
    13. file: rawFile,
    14. type: '101002',
    15. success: (res) => {
    16. this.src = res.url;
    17. this.form.attachmentId = res.id;
    18. this.$message({
    19. message: '上传成功',
    20. type: 'success'
    21. });
    22. this.$refs.upload.value = '';
    23. this.loading = false;
    24. },
    25. fail:(res) => {
    26. this.$message({
    27. message: res,
    28. type: 'error'
    29. });
    30. this.loading = false;
    31. this.$refs.upload.value = '';
    32. }
    33. })
    34. }

    封装上传方法 - util.uploadFile

    1. uploadFile({file, type, success= () => {}, fail= ()=> {}, baseUrl = '/common/upload'}) {
    2. let form = new FormData()
    3. form.append('file', file)
    4. if(type) {
    5. form.append('sourceType', type)
    6. }
    7. let url = process.env.BASE_URL + baseUrl
    8. axios({
    9. method: 'post',
    10. url,
    11. data: form,
    12. headers: {
    13. 'Content-Type': 'multipart/form-data',
    14. 'Authorization': localStorage.getItem('token'),
    15. }
    16. }).then(res => {
    17. if(res.data.code == 200) {
    18. return success(res.data.data)
    19. }else {
    20. return fail(res.data.message)
    21. }
    22. })
    23. },

    就这样就实现了自定义上传拉

    3.自定义下载

    下载文件,主要是要理解下载时后端返回的数据是什么,前端到底要怎么处理,可以去看看前面的文章链接,这里直接写封装的方法

    例如: 下载excel

    1. exportExcel(url, data, excelName){
    2. axios({
    3. method: 'post',
    4. url: process.env.BASE_URL + url,
    5. headers: {
    6. Authorization: localStorage.getItem('token')
    7. },
    8. responseType:'arraybuffer',
    9. data,
    10. }).then(res => {
    11. console.log(res, 'resss')
    12. const link = document.createElement('a')
    13. const fileName = excelName + ".xlsx";
    14. let blob = new Blob([res.data], {type: 'application/vnd.ms-excel'})
    15. link.style.display = 'none'
    16. link.href = URL.createObjectURL(blob)
    17. link.download = fileName //下载的文件名
    18. document.body.appendChild(link)
    19. link.click()
    20. document.body.removeChild(link)
    21. })
    22. }

  • 相关阅读:
    结合CAP理论分析ElasticSearch的分布式实现方式
    学习常见的反爬虫手段,如验证码、限制访问频率等
    【附源码】计算机毕业设计JAVA学生信息管理系统2021
    Doris:使用表函数explode实现array字段列转行
    pytorch中torch.mul、torch.mm、torch.matmul的区别
    论文导读 | 大语言模型与知识图谱复杂逻辑推理
    关于linux的一点好奇心(四):tail -f文件跟踪实现
    产品经理如何做数据分析
    CCF CSP认证 历年题目自练Day27
    CentOS7.9中使用packstack安装train版本
  • 原文地址:https://blog.csdn.net/qq_42625428/article/details/126399115