博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
前端-vue-文件上传(图片、word,ppt,pdf,excel,txt等文件流)
阅读量:4072 次
发布时间:2019-05-25

本文共 5318 字,大约阅读时间需要 17 分钟。

1. 文件上传(图片、word,ppt,pdf,excel,txt等文件流)

总结一下上传组件的思路

  • input标签的@change事件

  • 通过input标签的@change事件的绑定,进行文件的选择上传,获取到文件const uploadFile = file.target.files[0],然后通过文件流转为base64,然后可以在前端进行预览。

  • 预览只有图片和PDF

  • 预览的文件不多,只有img,和pdf的可以预览,这和浏览器可以打开的文件流有关,前端能做到的预览只有图片和PDF,像word、和ppt、excel等不可前端预览,需要后端解析,重新返回相应地址方可预览

  • word、和ppt、excel的预览需要后端做出相应的处理,这些文件返回的地址是处理过的,预览地址和下载地址是不一样的

  • 后端给的文件地址必须公网

  • 微软解析地址:https://view.officeapps.live.com/op/view.aspx?src=你的文件地址

  • 上传的子组件

  • 父组件使用:

  • 引入

    import addAttachments from '@/components/addAttachments/addAttachments.vue' import { apideleteAccessory } from '@/js/fileUpload/fileUpload.js'

  • 注册:components: { addAttachments },

  • 使用:<add-attachments ref="refaddattachments" @getattachmentList="getattachmentList"></add-attachments>

  • 父组件通过refs打开子组件,并且进行数据的获取等

// 添加附件    uploadfileclick () {      this.$refs.refaddattachments.openaddattachments()    },    // 获取数据    getattachmentList (arrlist) {      if (arrlist && arrlist.length > 0) {        this.attachmentList = this.attachmentList.concat(arrlist)      } else {        this.attachmentList = []      }    },    // 查看附件    seefileClick (item) {      window.open(item.url)    },    // 删除附件    deletefileClick (item, index) {      this.$confirm('是否确定删除?删除即将从数据库中删除!', '提示', {        confirmButtonText: '确定',        cancelButtonText: '取消',        type: 'warning'      }).then(() => {        apideleteAccessory(item.ADAccessoryID)          .then((res) => {            const result = res.data.result.resultObj            if (result && result.length > 0) {              if (result[0].flag === 'Y') {                // this.applyBillInfo.ADAccessory.splice(index, 1)                this.attachmentList.splice(index, 1)                this.applyBillInfo.ADAccessory = []                this.$message.success('删除附件成功!')              } else {                this.$message.error('删除附件失败!')              }            }          })          .catch((err) => {            this.$message.error('删除附件失败:' + err)          })      })    },    // 下载附件    downfileClick (item) {      window.open(item.downloadUrl)    }
  • 父组件的容器和触发方式
  • 附件
    添加
    添加
    {
    {item.fileName}}
    • 父组件容器的样式
// 附件            .accessory-content{                display: flex;                flex-direction: column;                justify-content: flex-start;                background:#F3F8FC;                padding: 11px 14px;                box-sizing: border-box;                .content-title{                    display: flex;                    justify-content: space-between;                    margin-bottom: 10px;                    .title-left{                        color: #303133;                        font-size: 14px;                        font-family: PingFangSC-Medium;                    }                    .title-right{                        cursor: pointer;                        color: rgba(0, 137, 255, 100);                        font-size: 14px;                        font-family: PingFangSC-Regular;                    }                }                .content-main{                    width: 100%;                    .content-attachment-list{                        display: flex;                        justify-content: space-between;                        align-items: center;                        height: 30px;                        line-height: 30px;                        .attachment-list-name{                            overflow: hidden;                            text-overflow: ellipsis;                            display: -webkit-box;                            -webkit-line-clamp: 1;                            -webkit-box-orient: vertical;                            width: 80%;                            text-align: left;                            color: rgba(96, 98, 102, 100);                            font-size: 14px;                            font-family: PingFangSC-Regular;                            cursor: pointer;                        }                        .attachment-list-right{                            display: flex;                            .attachment-list-btn{                                cursor: pointer;                                margin-right: 5px;                                .el-icon-delete,                                .el-icon-download{                                    width: 20px;                                    text-align: center;                                    font-size: 16px;                                    color: #0089FF;                                }                            }                        }                    }                }            }

最后的效果图

在这里插入图片描述

在这里插入图片描述

转载地址:http://whwni.baihongyu.com/

你可能感兴趣的文章
readUnsignedInt () 自动移动字节流位置,和.net是一样的
查看>>
大型应用程序中的资源destory办法
查看>>
action,webaction,mode,controller
查看>>
多个单例模式单例模式的应用
查看>>
北山白云里,隐者自怡悦。
查看>>
mouseChildren= false
查看>>
12个Flex常用功能代码
查看>>
addChild一个.swf时,该swf的背景色失效,同时如有超出大小的范围,也会显示,造成边距...
查看>>
MovieClip,Sprite,Shape三者之间的区别
查看>>
欣赏ActionScript 3 的元件架构
查看>>
在as3中只有事件(或该事件的子级)的发送者才能侦听事件
查看>>
rotation的单位是角度
查看>>
所谓速度就是每次移动比上次移动的距离多一点
查看>>
Flex Develpment中右边的框的linkWithEdit
查看>>
不兼容的签名实现和函数默认参数
查看>>
不兼容的签名实现,
查看>>
字体轮廓和设备字体
查看>>
水平和垂直翻转可视对象
查看>>
1.随机函数,计算机运行的基石
查看>>
MouseEvent的e.stageX是Number型,可见as3作者的考虑
查看>>