最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue el-upload手動(dòng)上傳實(shí)現(xiàn)過程

 更新時(shí)間:2024年06月12日 16:14:55   作者:不想996了的小姑娘  
這篇文章主要介紹了vue el-upload手動(dòng)上傳實(shí)現(xiàn)過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

vue el-upload手動(dòng)上傳

        ```vue
        <el-upload v-model="xlsFile" ref="fileUpload" :limit="1" accept=".xls, .xlsx" action="#" :multiple="false"
          :auto-upload="false" :before-upload="beforeFileUpload" :on-preview="preview" :on-change="fileChange"
          :http-request="uploadHttpRequest" :on-exceed="exceed" :on-remove="fileRemove" :on-success="fileSuccess">
          <el-button size="small" type="primary">
            導(dǎo)入文件
            <i class="el-icon-upload el-icon--right"></i>
          </el-button>
          <div class="el-upload__tip" style="color: rgba(255, 255, 255, 0.65)" slot="tip">
            提示:僅允許上傳多1個(gè)".xlsx"或者".xls"格式文件,單個(gè)文件最大10M
          </div>
        </el-upload>
         <el-button type="primary" @click="submitUpload()">上傳</el-button>
 script部分
```vue
                          // 試題導(dǎo)入
beforeFileUpload(file) {
 const isFileSizeLimit = file.size / 1024 / 1024 < 10;
 if (!isFileSizeLimit) {
   this.$message.error("單個(gè)圖片大小不能超過 10MB!");
 }
 return isFileSizeLimit;
},
// 文件添加、 上傳、 失敗
fileChange(file, fileList) {
 this.xlsFile = file;
 console.log("1111111", this.xlsFile);
},
// 文件上傳成功處理
fileSuccess(response, file, fileList) {
 // this.openUpload = false;
 // this.isUploading = false;
},
// 預(yù)覽文件
preview(file) {
 // this.dialogVisible = true;
},
// 文件超出限制
exceed(files, fileList) {
 this.$message.warning(
   `當(dāng)前限制選擇 1個(gè)文件,本次選擇了 ${files.length} 個(gè)文件,共選擇了 ${
     files.length + fileList.length
   } 個(gè)文件`
 );
},
// 移除文件
fileRemove(file, fileList) {
 this.xlsFile = null;
},
// 點(diǎn)擊上傳
submitUpload() {
 this.$refs.fileUpload.submit();
},
uploadHttpRequest(param) {
 const formData = new FormData(); //FormData對(duì)象,添加參數(shù)只能通過append('key', value)的形式添加
 formData.append("file", this.xlsFile.raw); //添加文件對(duì)象
 // this.$refs["form"].validate((valid) => {
   // if (valid) {
   //   if (!this.form.id) {
   //     formData.append("passValue", this.form.passValue);
   //     formData.append("scoreEvery", this.form.scoreEvery);
   //     formData.append("testName", this.form.testName);
   //   } else {
   //     formData.append("id", this.form.id);
   //   }
     excelData(formData)
       .then((res) => {
         if (res.code === 200) {
           this.msgSuccess("導(dǎo)入成功");
           console.log('re====>',res.data)
           // this.open = false;
           this.getList();
         }
       })
       .catch((err) => {
         console.log("失敗", err);
         param.onError(); //上傳失敗的文件會(huì)從文件列表中刪除
       });
   // }
 // });
},

把el-upload里的自動(dòng)上傳auto-upload置為false,然后自定義上傳按鈕,調(diào)用

this.$refs.fileUpload.submit();

便會(huì)觸發(fā) :

http-request=“uploadHttpRequest”

vue在表單中使用el-upload手動(dòng)上傳圖片

自動(dòng)上傳和手動(dòng)上傳

上傳圖片分兩種,自動(dòng)上傳和手動(dòng)上傳,效果區(qū)別:

  • 自動(dòng)上傳:選擇圖片后立刻調(diào)接口上傳圖片
  • 手動(dòng)上傳:選擇圖片后只顯示圖片,自定義何時(shí)上傳,可以定義點(diǎn)擊確定事件里表單驗(yàn)證成功后上傳圖片
  • 區(qū)別:自動(dòng)上傳會(huì)造成很多臟數(shù)據(jù),手動(dòng)上傳等到上傳時(shí)才校驗(yàn)圖片是否合規(guī)

手動(dòng)上傳

表單中使用el-upload手動(dòng)上傳圖片,組件選擇的是照片墻

<template>
    <el-form
      ref="cardFormRef"
      :model="cardForm"
      :rules="rules"
      label-width="120px"
      class="demo-cardForm"
      status-icon
    >
    <el-form-item label="輪播圖" prop="photo">
        <el-upload
          ref="uploadRef"
          :class="{ iconVis: fileList.length }"
          :action="url" //上傳接口
          v-model:file-list="fileList"
          :limit="1" //限制上傳一張
          list-type="picture-card"  //照片墻
          :before-upload="beforeUpload"  //上傳前
          :on-success="handleAvatarSuccess"  //上傳成功
          :headers="headers"
          :auto-upload="false"  //手動(dòng)上傳
        >
          <el-icon class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
  </el-form>
</template>
var fileList = ref([]);
var uploadUrl = ref(false); //存圖片成功返回的url
const headers = ref({ Authorization: "Bearer " + getToken() });
var url =import.meta.env.VITE_APP_BASE_API + "接口";
 
var rules = computed(() => ({  //表單校驗(yàn)規(guī)則
  photo: [
    {
      required: true,
      message: "請(qǐng)上傳圖片",
      trigger: "blur",
    },
  ]
}));
 
var beforeUpload = (file) => {
  console.log("上傳前");
  const isJPG =
    file.type === "image/jpeg" ||
    file.type === "image/png" ||
    file.type === "image/jpg";
  const isLt2M = file.size / 1024 / 1024 < 2;
  if (!isJPG) {
    proxy.$modal.msgError("上傳圖片只能是 JPG/PNG 格式!");
  }
  if (!isLt2M) {
    proxy.$modal.msgError("上傳圖片大小不能超過 2MB!");
  }
  isJPG && isLt2M ? (uploadUrl.value = true) : (uploadUrl.value = false);
  return isJPG && isLt2M;
};
 
function handleAvatarSuccess(res, file) {
  console.log("成功了!");
  let { url } = res.data;
  uploadUrl.value = url;
  sumbitForm(); //表單提交接口,傳uploadUrl
}
 
var cardFormRef=ref(null);
var uploadRef=ref(null);
var sumbit = () => {  //點(diǎn)擊確定按鈕,進(jìn)行表單校驗(yàn),校驗(yàn)成功上傳圖片
  cardFormRef.value.validate((val) => {
    if (val) {
      console.log("上傳圖片");
     uploadRef.value.submit();
    }
  });
};

點(diǎn)擊確定sumbit,表單校驗(yàn)成功 => beforeUpload檢查圖片符合規(guī)格 => handleAvatarSuccess圖片上傳成功 =>sumbitForm提交表單,包含圖片上傳成功返回的url

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

灵寿县| 兴化市| 正宁县| 广德县| 荆州市| 汝城县| 精河县| 灵璧县| 稷山县| 新疆| 微博| 探索| 孟连| 治县。| 双柏县| 商城县| 仙游县| 乌什县| 南平市| 财经| 全州县| 镶黄旗| 冷水江市| 珲春市| 旺苍县| 深水埗区| 广昌县| 赤城县| 黄山市| 会泽县| 阿克陶县| 芦溪县| 德兴市| 惠东县| 水富县| 建昌县| 浦城县| 高州市| 定安县| 桂平市| 临湘市|