elementui upload组件自定义表格上传及下载

最近一个后台项目后端新增了一个接口,将excel模板文件作为参数,返回是填过数据后的表格文件流;前端需要增加一个上传按钮,实现表格文件的上传及下载。
elementui中有upload组件,可以直接实现上传功能,但是无法对返回的文件流进行合适的处理,所以用组件中的http-request属性来自定义文件上传的方式,更灵活。

1. HTML结构
<el-popover placement="right" title="注意:" content="上传文件的格式请参考excel模板!" trigger="hover">
    <el-upload slot="reference" :http-request="fileUpload" :show-file-list="false" :limit="1" accept=".xls,.xlsx" class="upload" drag action="">
      <i class="el-icon-upload"/>
      <div class="el-upload__text">将exel文件拖到此处,或<em>点击上传</em></div>
    </el-upload>
</el-popover>

其中,http-request属性绑定的fileUpload方法就是自定义上传方法,action是必填项,这里可以填为空。

2. 自定义fileUpload方法
fileUpload(param) {
      const fileObj = param.file
      const fd = new FormData()  // 需要将文件转换为文件流
      fd.append('file', fileObj)
      axios({
        method: 'post',
        url: `${baseAPI[NODE_ENV]}futures-analysis/analysis/test`,
        data: fd,
        headers: {
          'Content-Type': 'multipart/form-data'  // 设置请求头
        },
        responseType: 'blob'  // 这里的响应类型要设置为blob,不然得到的响应是乱码,转换不了
      }).then(res => {
        // console.log(res)
        const link = document.createElement('a')  // 创建一个虚拟结点
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
        link.style.display = 'none'
        link.href = URL.createObjectURL(blob)
        link.setAttribute('download', '文件名.xlsx')
        document.body.appendChild(link)
        link.click() // 模拟点击下载的事件
        document.body.removeChild(link)
      }).catch(error => {
        console.log(error)
      })
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现vue-quill-editor配合elementui上传组件自定义图片上传操作,可以使用以下步骤: 1. 在vue-quill-editor中配置图片上传的方法: ``` <template> <quill-editor v-model="content" ref="myQuillEditor" :options="editorOption"></quill-editor> </template> <script> import { quillEditor } from 'vue-quill-editor' export default { components: { quillEditor }, data() { return { content: '', editorOption: { modules: { toolbar: [ ['bold', 'italic', 'underline', 'strike'], ['blockquote', 'code-block'], [{ 'header': 1 }, { 'header': 2 }], [{ 'list': 'ordered' }, { 'list': 'bullet' }], [{ 'script': 'sub' }, { 'script': 'super' }], [{ 'indent': '-1' }, { 'indent': '+1' }], [{ 'direction': 'rtl' }], [{ 'size': ['small', false, 'large', 'huge'] }], [{ 'header': [1, 2, 3, 4, 5, 6, false] }], [{ 'color': [] }, { 'background': [] }], [{ 'font': [] }], [{ 'align': [] }], ['clean'], ['image'] ], imageUploader: { upload: file => { // 自定义图片上传 } } }, placeholder: '请输入内容', theme: 'snow' } } } } </script> ``` 在`modules`中添加`imageUploader`属性,用于自定义图片上传操作,其中`upload`方法就是自定义的图片上传方法。 2. 在自定义的图片上传方法中,使用elementui上传组件进行图片上传: ``` <template> <el-upload class="avatar-uploader" :action="uploadUrl" :show-file-list="false" :on-success="handleSuccess"> <img v-if="imageUrl" :src="imageUrl" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </template> <script> export default { data() { return { imageUrl: '', uploadUrl: '/api/upload' } }, methods: { handleSuccess(response) { // 上传成功后,获取图片地址,然后插入到编辑器中 const url = response.url const quillEditor = this.$refs.myQuillEditor.quill const range = quillEditor.getSelection(true) const index = range.index + range.length quillEditor.insertEmbed(index, 'image', url) } } } </script> ``` 3. 在handleSuccess方法中,获取上传成功后的图片地址,然后通过`quillEditor.insertEmbed`方法将图片插入到编辑器中。 这样,就可以实现vue-quill-editor配合elementui上传组件自定义图片上传操作了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值