el-upload上传文件,如何使用action、auto-upload

1点击打开自动上传

没有

:auto-upload="false"就是自动上传

2点击上传按钮上传

不自动上传:auto-upload="false"

<el-dialog
        style="background: rgb(18, 67, 112); border: 1px solid #409eff"
        :title="上传文件"
        :model-value="dialogVisible"
        :before-close="cancelUpload"
      >
        <el-upload
          class="upload-demo"
          drag
          action="/接口地址"
          multiple
          :auto-upload="false"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :file-list="fileList"
          ref="upload"
        >
          <el-icon class="el-icon--upload"><upload-filled /></el-icon>
          <div class="el-upload__text"> 拖拽到此或<em>点击上传</em> </div>
          <!--                    <template #tip>-->
          <!--                      <div class="el-upload__tip"> 只能上传..文件,且不超过500kb </div>-->
          <!--                    </template>-->
        </el-upload>
        <template #footer>
          <span class="dialog-footer">
            <el-button type="primary" class="long_table" @click="submitUpload()">上 传</el-button>
          </span>
        </template>
      </el-dialog>

对话框相关

el-dialog

:model-value="dialogVisible" 绑定对话框的是否打开
:before-close="cancelUpload" 点击×关闭对话框事件

上传相关

el-upload

action="接口地址"  绑定接口地址
:auto-upload="false" 不自动上传
:file-list="fileList"  这个list的显示

ref="upload"  实现后面点击上传用的

ps记得定义元素 后面省略

 const state = reactive({
        fileList: [],
        dialogVisible: false,
        upload: '',

      function cancelUpload() {
        state.dialogVisible = false;
        state.fileList = [];
        getbookList();
      }
function handleRemove(file, fileList) {
  console.log(file, fileList);
}
function handlePreview(file) {
  console.log(file);
}
function submitUpload(e, file, fileList) {
  state.upload.submit(); //将文件上传
  ElMessage({
    message: '上传成功',
    type: 'success',
    duration: '1000',
  });
}

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 `el-upload` 组件来实现文件上传功能。`el-upload` 是一个基于 Element-UI 的组件,可以方便地实现文件上传和文件预览的功能。 首先,确保你已经安装了 Element-UI。然后,在你的项目中引入 `el-upload` 组件,并按照以下步骤配置: 1. 在需要使用上传组件的页面中,添加 `el-upload` 标签。例如: ```html <template> <div> <el-upload class="upload-demo" action="/your-upload-url" :on-success="handleSuccess" :before-upload="beforeUpload" :limit="3" :on-exceed="handleExceed" :auto-upload="false" > <el-button size="small" type="primary">点击上传</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> </div> </template> ``` 2. 在 `el-upload` 的属性中配置上传相关的参数,例如: - `action`:设置上传文件的接口地址。 - `on-success`:设置上传成功后的回调函数。 - `before-upload`:设置上传前的钩子函数,用于对文件进行校验或其他操作。 - `limit`:设置最大允许上传的文件数量。 - `on-exceed`:设置超出限制时的回调函数。 - `auto-upload`:设置是否自动上传文件。 3. 在组件的 `methods` 中定义相关函数,例如: ```javascript methods: { handleSuccess(response, file, fileList) { // 上传成功后的处理逻辑 console.log(response, file, fileList); }, beforeUpload(file) { // 文件上传前的校验逻辑 const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'; const isLt2M = file.size / 1024 / 1024 < 2; if (!isJPG) { this.$message.error('只能上传 JPG/PNG 格式的图片'); } if (!isLt2M) { this.$message.error('上传图片大小不能超过 2MB'); } return isJPG && isLt2M; }, handleExceed(files, fileList) { // 超出文件数量限制时的处理逻辑 this.$message.warning(`当前限制选择 ${this.limit} 个文件,本次选择了 ${files.length} 个文件,共选择了 ${fileList.length + files.length} 个文件`); } } ``` 以上是一个简单的 `el-upload` 组件的配置示例。你可以根据自己的需求进行修改和扩展。希望对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值