草庐IT

【Element UI】el-upload 踩的坑

chase… 2023-04-11 原文

1、action

上传的地址,接口地址
直接在 action 中写后端地址会出现跨域问题,而且传参数不方便
就用 http-request 指定具体上传方法

2、auto-upload

是否在选取文件后立即进行上传,默认 true
在 action 赋空值,使用 http-request 指定方法上传时,auto-upload 为 false

3、http-request

覆盖默认的上传行为1,可以自定义上传的实现
默认的上传方法均失效,获取不到 file 值
需要使用 on-change2 做上传文件前的处理

4、上传文件显示进度条

el-progress3

5、上传 .xls , .xlsx 文件并显示进度条的实现代码

<el-dialog
  ref=""
  append-to-body
  :title="excel.title"
  v-if="excel.visible"
  :close-on-click-modal="false"
  :visible.sync="excel.visible"
>
  <el-upload
    class="upload-demo"
    ref="upload"
    action=""
    :limit="1"
    :auto-upload="false"
    :file-list="excel.upload.fileList"
    :on-change="excelChange"
    :http-request="excelRequest"
    :on-remove="excelRemove"
  >
    <el-button icon="el-icon-upload2" plain @click="excelReset">{{ st('frame.choiceFile') }}</el-button>
    <div slot="tip" class="el-upload__tip">只能上传 .xlsx 和 .xls 文件,且不超过1个文件</div>
  </el-upload>
  <el-progress v-show="excel.progressFlag" :percentage="excel.loadProgress"></el-progress>
  <div ref="uploadFile"></div>
  <div slot="footer" class="dialog-footer">
    <el-button type="primary" @click="cancel()">{{st('frame.cancel')}}</el-button>
    <el-button type="success" @click="submitFile()">{{st('publicCustom.ok')}}</el-button>
  </div>
</el-dialog>
data() {
	return {
      excel: {
        title: this.st('frame.import'),
        visible: false,
        progressFlag: false,
        loadProgress: 0,
        upload: {
          fileList: [],
          action: '',
          headers: {},
          data: {
            jsondata: ''
          }
        }
      }
   }
}
methods: {
	excelReset() {
      this.excel.upload.fileList = []
      this.$refs.uploadFile.innerHTML = null
    },
    excelRemove() {
      this.excel.upload.fileList = []
      this.excel.progressFlag = false
      this.$refs.uploadFile.innerHTML = null
      this.excel.loadProgress = 0
    },
    excelChange(file) {
      if (file.name.indexOf('.xlsx') == -1 && file.name.indexOf('.xls') == -1) {
        this.$message.error(this.st('frame.uploadError'))
        this.excel.upload.fileList = [] 
      } else {
        if(file.status === 'ready'){
          this.excel.progressFlag = true
          this.excel.loadProgress = 0
          const interval = setInterval(() => {
            if(this.excel.loadProgress >=99){
              clearInterval(interval)
              return
            }
            this.excel.loadProgress += 1
          }, 20)
        }
        this.excelRequest(file)
      }
    },
    excelRequest(file) {
      var form = new FormData()
      form.append("file", file.raw)
      InportPbomPartExcel(form).then((res) => {
        if (res.resultData.success) {
          const url = settings.api.url + res.resultData.fileName
          window.open(url)
          let template = res.resultData.MessageString
          this.$refs.uploadFile.innerHTML = template
          this.excel.progressFlag = false
          this.excel.loadProgress = 100
        } else {
          this.$message.error(res.resultData.MessageString)
        }
      })
    }
}

  1. on-preview 点击文件列表中已上传的文件时的钩子
    on-success 文件上传成功时的钩子
    on-progress 文件上传时的钩子
    before-upload 上传文件之前的钩子,参数为上传的文件,若返回 false 或者返回 Promise 且被 reject,则停止上传。 ↩︎

  2. on-change 文件状态改变时的钩子,添加文件、上传成功和上传失败时都会被调用 ↩︎

  3. <el-upload 
     action="Fake Action" :before-upload="uploadSuccess" :on-change="uploadVideoProcess" :show-file-list="false" :file-list="fileList">
     <el-button v-if="typePage !=='view'" size="mini" type="primary">点击上传</el-button>
     <span v-if="typePage !=='view'" slot="tip" class="el-upload__tip">支持pdf,jpg,png格式文件</span>
    </el-upload>
    <el-progress v-show="progressFlag" :percentage="loadProgress"></el-progress>
    
    uploadVideoProcess(file, fileList) { 
       if(file.status === 'ready'){
          this.progressFlag = true; // 显示进度条
          this.loadProgress = 0; 
          const interval = setInterval(() => {
            if(this.loadProgress >=99){
              clearInterval(interval)
              return
            }
            this.loadProgress += 1
          }, 20);
        }
      if (file.status === 'success') {
        this.progressFlag = false; // 不显示进度条
        this.loadProgress = 100;
      }
    }
    
    ↩︎

有关【Element UI】el-upload 踩的坑的更多相关文章

  1. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  2. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  3. ruby-on-rails - carrierwave:在序列化动态属性上安装 uploader - 2

    首先,我使用的是rails3.1.3和来自master的carrierwavegithub仓库的分支。我使用after_init钩子(Hook)来确定基于属性的字段页面模型实例并为这些字段定义属性访问器将值存储在序列化哈希中(希望它清楚我是什么谈论)。这是我正在做的事情的精简版:classPage省略mount_uploader命令让我可以访问我想要的属性。但是当我安装uploader时出现错误消息说“nil类的未定义新方法”我在源代码中读到有方法read_uploader和扩展模块中的write_uploader。我如何必须覆盖这些来制作mount_uploader命令使用我的“虚拟

  4. ruby-on-rails - 带图片 uploader 的多步表单 - 2

    我想建立3步用户注册,在第2步上传头像。所以我遵循RyanBates的指南http://railscasts.com/episodes/217-multistep-forms.我正在使用CarrierWavegem来处理上传。但似乎我无法在用户session中存储上传的文件信息(我收到无法转储文件错误)。我在Controller中使用以下技术ifparams[:user][:img_path]@uploader=FirmImgUploader.new@uploader.store!(params[:user][:img_path])session[:img]=@uploaderpara

  5. ruby - 手动更新 Carrierwave Uploader 安装的属性 - 2

    我无法对由载波uploader装载的属性使用model.update_attribute。SQL语句不会接受该值并将NULL添加到占位符。如果我从模型类中删除mount_uploader语句,它会正常工作。我正在从控制台进行故障排除并尝试在为数据库播种时添加一些属性,这阻碍了我的努力。想法?谢谢。更新:相关代码:classProfile我只是想从数据库种子文件重写:avatar字符串,同时从Rails控制台进行测试,如下所示:Profile.first.update_attribute(:avatar,'foo')当我注释掉mount_uploader行时,两者都有效。添加mount_

  6. ruby-on-rails - 在 El Capitan 上安装 Rails 时出现 -lgmp 错误的库未找到(Mac OS 10.11.1 (15B42)) - 2

    在使用Rubyv2.2.2的ElCapitan(MacOSX10.11.1)上安装Rails时,出现以下错误:ERROR:Errorinstallingnokogiri:ERROR:Failedtobuildgemnativeextension./Users/jon/.rvm/rubies/ruby-2.2.2/bin/ruby-r./siteconf20151117-26799-ux15fd.rbextconf.rb--use-system-librariescheckingiftheCcompileraccepts...***extconf.rbfailed***Couldnotc

  7. Java调用ffmpeg处理视频,并记录下遇到的坑 - 2

    目录需求基于JavaCV跨平台执行ffmpeg命令[^1]坑一内存不足坑二多个ffmpeg进程并行导致IO负载大,进而导致ioerror?坑三使用Java操作ffmpeg时,有时会卡死坑四Process的waitFor死锁问题及解决办法需求给透明背景的视频自动叠加一张背景图片基于JavaCV跨平台执行ffmpeg命令1我测试发现的本需求的最小依赖:dependency>groupId>org.bytedecogroupId>artifactId>ffmpeg-platform-gplartifactId>version>5.0-1.5.7version>dependency>核心代码:Stri

  8. ruby - gem install rmagick 在 OS X El Capitan 上失败 - 2

    几天前我升级到ElCapitan并运行了一个brewupdate&&brewupgrade它更新了imagemagick,导致ruby​​的rmagickgem停止工作。我想没问题,我就跑geminstallrmagick它会重新编译。除了没有,当我运行它时我看到了这个:geminstallrmagickBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingrmagick:ERROR:Failedtobuildgemnativeextension./Users/sam/.rbenv/versions/2.

  9. ruby-on-rails - 未定义方法 `upload' 用于 nil :NilClass Did you mean? 加载 - 2

    尝试将ActiveStorage用于简单的图像上传表单。它创建成功,但在提交时抛出错误:undefinedmethod`upload'fornil:NilClassDidyoumean?load这是它要我查看的block:@comment=Comment.create!params.require(:comment).permit(:content)@comment.image.attach(params[:comment][:image])redirect_tocomments_pathend这是在完整的Controller中:classCommentsController实际应该发

  10. ruby-on-rails - Carrierwave - "uploading"来自字符串的文件 - 2

    我网站中的用户可以上传自己的模特照片或从图库中选择。当用户从库中选择时,我将文件名作为字符串发送到服务器{file:{url:'url.jpg'}}。我还没有找到carrierwave可以只更新模型文件而不上传它的方法。我可以在我的模型中编写一个条件来检查该参数是否存在,然后是model.file=File.open('str.jpg')。从安全角度来看,这不好吗?我如何才能“上传”文件,或仅更新文件属性,以引用服务器上已有的文件?谢谢! 最佳答案 您使用File.open的解决方案可行,但您应该使用File.basename验证该

随机推荐