element ui 上传图片以及pdf组件

这篇具有很好参考价值的文章主要介绍了element ui 上传图片以及pdf组件。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

FileUpload.vue

<template>
  <div>
    <el-upload
      :action="action"
      :file-list="fileList"
      list-type="picture-card"
      :limit="limit"
      :accept="accept"
      :class="hideUpload || uploading ? 'hideUpload' : ''"
      :on-error="handleError"
      :before-upload="beforeUpload"
      :on-success="handleImageSuccess"
      multiple
    >

      <i slot="default" class="el-icon-plus"></i>

      <div slot="file" slot-scope="{ file }">
        <div>

        <img
        class="el-upload-list__item-thumbnail"
        :src="isPDF(file.url) ? pdf : file.url"
        alt="" />

        <span
          v-if="file.status === 'success'"
          class="el-upload-list__item-actions"
        >
          <span
            v-if="preview"
            class="el-upload-list__item-preview"
            @click="handlePreview(file)"
          >
            <i class="el-icon-zoom-in"></i>
          </span>
          <span
            v-if="download"
            class="el-upload-list__item-delete"
            @click="handleDownload(file)"
          >
            <i class="el-icon-download"></i>
          </span>
          <span
            v-if="deleted"
            class="el-upload-list__item-delete"
            @click="handleRemove(file)"
          >
            <i class="el-icon-delete"></i>
          </span>
        </span>
        <span
          v-else
          :class="[
            uploading ? 'uploading' : '',
            'el-upload-list__item-actions',
          ]"
        >
          <i class="el-icon-loading" /><i style="font-size: 14px">上传中</i>
        </span>


      </div>

      </div>
    </el-upload>

    <el-dialog :visible.sync="previewVisible">
      <img width="100%" :src="previewImgUrl" alt="" />
    </el-dialog>
  </div>
</template>

<script>

import logo from '@/assets/pdf.png'

export default {
  name: 'FileUpload',
  props: {
    value: {
      type: [String, Array],
      default: ''
    },
    // 上传的地址
    action: {
      type: String,
      default: '', //上传接口
    },
    // 设置上传的请求头部
    headers: {
      type: Object,
      default: () => {
        return {}
      },
    },
    // 上传文件大小限制, 默认 50M,单位M
    size: {
      type: [Number, String],
      default: 50,
    },
    // 文件上传格式, 默认jpeg, png,jpg
    accept: {
      type: String,
      default: 'image/jpeg,image/png',
    },
    // 是否显示删除操作按钮
    deleted: {
      type: Boolean,
      default: true,
    },
    // 是否显示预览操作按钮
    preview: {
      type: Boolean,
      default: true,
    },
    // 是否显示下载操作按钮
    download: {
      type: Boolean,
      default: true,
    },
    // 上传文件个数限制,默认0 不限制
    limit: {
      type: [Number, String],
      default: 0,
    },
  },
  data() {
    return {
      fileList: [], // 默认文件列表
      hideUpload: false, // 超出限制掩藏上传按钮
      uploading: false, // 是否上传中,上传时隐藏上传按钮
      previewImgUrl: '', // 预览图片地址
      previewVisible: false, // 是否显示预览
      pdf: logo,
      files: [], // 文件url数组
    }
  },
  watch:{
    value: {
        handler(val, old){
          console.log('FileUpload=', val)
          if(val){
            this.files = val
            this.fileList = [] // 先清空
            for(var i=0; i<val.length;i++){
              this.fileList.push({
                url: val[i] // 转化
              })
            }
            this.handleChange()
          }
      },
      deep: true,
      immediate: true // 避免在子组件中监听失效
    }
  },
  methods: {
    emitInput() {
      this.$emit('input', this.files)
    },
    // 判断是否pdf
    isPDF(url) {
      if(!url){
        return false
      }
      const fileType = ['pdf']
      const index = url.lastIndexOf('.')
      const type = url.substr(index + 1)
      return fileType.indexOf(type) > -1
    },
    // 文件上传成功
    handleImageSuccess(res) {
      if (res.code === 200) {
        this.files.push(res.data.file)
        this.emitInput()
      } else {
        this.$message.error('文件上传失败')
      }
    },
    // 上传前文件大小判断
    beforeUpload(file) {
      const { size } = this
      const overSize = size > 0 && file.size < 1024 * 1024 * size
      if (!overSize) this.$message.error(`上传文件大小不能超过 ${size}MB!`)
      this.uploading = overSize // 是否上传中
      return overSize
    },
    // 上传出错返回
    handleError(event, file, fileList) {
      console.log(event, file, fileList, 'error')
      this.$message.error('服务出错,上传失败!')
      this.handleChange()
    },
    // 删除图片
    async handleRemove(file) {
      this.$confirm(`确认删除文件?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { fileList } = this
        this.files = this.files.filter((v) => v !== file.url)
        this.emitInput()
      }).catch(()=>{})
    },
    // 图片预览
    handlePreview(file) {
      if(this.isPDF(file.url)){
        window.open(file.url, "_blank");
      } else {
        this.previewImgUrl = file.url
        this.previewVisible = true
      }
    },
    handleChange(file, list) {
      const { limit, fileList } = this
      if (limit > 0 && fileList.length >= limit) this.hideUpload = true
      else this.hideUpload = false
      this.uploading = false
    },
    handleDownload(file) {
      window.open(file.url, "_blank");
      /*const a = document.createElement('a')
      a.href = file.url
      a.click() // 模拟点击事件,实现图片文件的同源下载
      */
    },
  },
}
</script>

<style lang="scss" scoped>
.hideUpload .el-upload--picture-card {
  display: none;
}
.el-upload-list--picture-card .uploading.el-upload-list__item-actions {
  opacity: 1;
}
/*添加、删除文件时去掉动画过渡*/
.el-upload-list__item {
  transition: none !important;
}

.el-upload-list--picture-card .el-upload-list__item-thumbnail {
    width: 148px;
    height: 148px;
}
.el-upload-list--picture-card .el-upload-list__item {
    background-color: inherit;
    border: none;
    width: 148px;
    height: 148px;
}
</style>
 

在main.js中引入并全局注册:

import FileUpload from '@/components/upload/FileUpload.vue'

Vue.component('FileUpload', FileUpload) // 图片/pdf 添加和编辑

使用:

 <div>
        <FileUpload
        v-model=""  //绑定数据
        accept=".jpg, .jpeg, .png, .pdf"
        />
      </div>文章来源地址https://www.toymoban.com/news/detail-572593.html

到了这里,关于element ui 上传图片以及pdf组件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 基于vue element-ui 封装上传图片组件 功能:上传,删除,预览,上传图片水印,拖拽排序,上传进度条等

    我们在开发后台时肯定避免不了上传图片的功能 例如: 上传图片回显 上传完成 : 预览查看 , 删除等 如果是图片列表,还可能让你拖动图片排序 有的后台项目可能要给图片添加水印,添加标记 有的后台可能要炫酷一点 添加进度条功能 现在我们要完成上面的一系列功能,这里我

    2024年02月16日
    浏览(45)
  • Java将获取的参数,图片以及pdf文件放入到word文档指定位置

    首先引入的依赖 接下面的是template.docx文档,参数是以{{paramName}}格式的,为什么要以这种格式,是因为下面的方法,在替换参数的时候需要 但是从数据库获取的参数跟模板中的参数一一对应上即可,格式如下(我是json形式展示的): {     \\\"countQuota\\\": \\\"1\\\",     \\\"noEmission\\\": \\\"1\\\",  

    2024年02月15日
    浏览(38)
  • Element UI结合vue-cropper打造图片裁剪上传组件

    props: { //图片裁切配置 options: { type: Object, default: function() { return { autoCrop: true, //是否默认生成截图框 autoCropWidth: 180, //默认生成截图框宽度 autoCropHeight: 180, //默认生成截图框高度 fixedBox: false, //是否固定截图框大小 不允许改变 previewsCircle: true, //预览图是否是原圆形 title: ‘修改

    2024年04月10日
    浏览(28)
  • 基于element-ui封装上传图片到腾讯云Cos组件

    组件需求 上传图片到腾讯云Cos服务器 可以显示传入的图片地址 可以删除传入的图片地址 可以上传图片到云服务器 上传到腾讯云之后,可以返回图片地址,显示 上传成功之后,可以回调成功函数 需要使用借助一个插件,帮助我们上传图片资源到腾讯云Cos 使用element的el-upl

    2023年04月17日
    浏览(78)
  • 后端返回二进制流,前端处理二进制文件流,实现预览图片以及PDF

    1、首先预览PDF需要 后端 将响应头 Content-Type 设置为PDF类型 application/pdf ,不能预览,会直接下载 2、 前端 定义接口:并设置相应类型 responseType 为 blob 请求数据:通过 window.URL.createObjectURL(res) 转成本地预览地址, 在通过 window.open() 方法打开转成本地预览地址即可预览PDF,如下

    2024年02月15日
    浏览(37)
  • vue 集成tinymce2实现图片,视频以及文件的上传

    1. 安装插件 (1)安装tinymce npm install tinymce -S (2)安装tinymce-vue npm install @tinymce/tinymce-vue@3.0.1 -S 2. 复制静态文件到 public 目录 资源下载路径:https://download.csdn.net/download/weixin_44021888/88063970?spm=1001.2014.3001.5503 3. 新建组件:tinymce 注意:如果上传过后的视频,只有一张图片的占位

    2024年02月16日
    浏览(27)
  • 【SpringBoot】简单的文件上传和文件下载以及图片回显

    介绍 这里是小编成长之路的历程,也是小编的学习之路。希望和各位大佬们一起成长! 以下为小编最喜欢的两句话: 要有最朴素的生活和最遥远的梦想,即使明天天寒地冻,山高水远,路远马亡。 一个人为什么要努力? 我见过最好的答案就是:因为我喜欢的东西都很贵,

    2024年01月15日
    浏览(28)
  • AndroidStudio-图片的上传以及存进mysql数据库里

    参考文章: 如何简单地利用BITMAP为中介储存图片到数据库中 android开发实现头像上传功能 先添加Tiny框架的依赖 然后创建dialog的xml文件dialog_select_photo 然后创建一个空白的activity,在该activity的xml里添加一个按钮(btn_test)和一个ImagView(image) 然后是activity里的代码,有些依赖可能会

    2023年04月23日
    浏览(24)
  • Java使用ftl模板文件生成Word,以及Word转换图片或Pdf工具类

    一、写在前面 最近在项目中使用打印功能,发现这个功能我已经写过多次了,下面这个文章的发步日期在2020年,不得不感慨时间之快啊。 https://blog.csdn.net/weixin_43238452/article/details/109636200?spm=1001.2014.3001.5501 下面介绍一下应用场景:这次项目依旧是springboot项目,使用ftl模版生

    2024年02月15日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包