华为云OBS文件上传和下载

华为云Obs文件上传和下载

使用的技术

  1. 前端是Vue框架,element-ui
  2. 后端是Springboot项目
  3. 服务器是华为云
  4. 文件上传下载地方是华为云Obs对象存储服务

1、前端上传代码

el-upload组件:

<el-upload
    class="upload-demo"
    ref="upload"
    action
    :limit="limitNum"
    :before-upload="beforeAvatarUpload"
    :on-remove="handleRemove"
    :http-request="myUpload"
    :file-list="fileList"
    :auto-upload="false">
    <el-button slot="trigger" size="small" type="primary">选择文件</el-button>
    <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
    <div slot="tip" class="el-upload__tip">服务器容量有限,尽量不要上传大文件!</div>
  </el-upload>

方法:

methods: {
   
   
      // 手动上传操作
      submitUpload() {
   
   
        // 触发自定义的上传方法 myUpload
        this.$refs.upload.submit();
      },
      // 覆盖默认的上传行为,自定义上传的实现,有几个文件就会调用这个方法几次
      myUpload(fileObj) {
   
   
        let formData = new FormData();
        formData.set('file', fileObj.file);
        this.$api.fileUpload(formData)
          .then(response => {
   
   
            if (response.data.success === true) {
   
   
              this.$notify({
   
   
                title: '上传成功',
                message: response.data.re.name + '上传成功!',
                type: 'success'
              });
              // 清空已上传的文件列表
              this.$refs.upload.clearFiles();
            } else {
   
   
              this.$notify.error({
   
   
                title: '上传失败',
                message: '上传失败,请重试!'
              });
            }
          })
          .catch(error => {
   
   
            this.$message('发生错误了!'
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值