美文网首页
vue -el-upload上传文件的几种方式

vue -el-upload上传文件的几种方式

作者: 温柔vs先生 | 来源:发表于2023-10-18 10:30 被阅读0次
    <el-upload
                  class="upload-box"
                  action="/anylang-api/cos/uploadFile"
                  accept=".mp4,.mov"
                  :headers="{ token }"
                  :show-file-list="false"
                  :before-upload="handleBeforeUpload"
                  :on-success="handleUploadSuccess"
                >
                  
                    <div class="icon-upload">
                      <img class="img" src="@/assets/img/videoImg.png" alt="" />
                    </div>
                    <div class="upload-file-info">Amazon business success.mp4</div>
                    <div class="reUpload" style="margin-top: 52px">Reupload</div>
                </el-upload>
    
    handleBeforeUpload(file) {
          console.log(file);
          const suffix = file.name.split(".").pop().toLowerCase();
          if (!["mov", "mp4"].includes(suffix)) {
            return this.$message.error(`Please select mp4/mov extension file.`);
          }
          // 大小不能大于500M
          if (file.size / 1024 / 1024 >= 500) {
            this.$message.error(
              "The size of the image must not be greater than 500M"
            );
          }
        },
        handleUploadSuccess(res) {
          this.isTranslating = false;
          console.log(res);
          if (res.code == 200 || res.code == "200") {
            this.uploadVideoUrl = res.data;
          }
        },
         
    
    <el-upload class="selfVideoUpload" ref="uploadFile" :show-file-list="false"
                            :before-upload="beforeAvatarUpload" :http-request="uploadAvatarFile" :limit="1" accept=".xls,.xlsx">
    
                            <div v-if="form.action.length > 0"
                                style="display: flex; flex-direction: column; align-items: center;justify-content: center;">
                                <span style="display: flex; align-items: center;justify-content: center;">
                                    <img class="uploadIcon" src="@/assets/imgs/icon-file-default@2x.png" />
                                    <span class="videoTitle">批量上传</span>
                                </span>
    
                                <div style="display: flex; align-items: center;justify-content: center; color: #DCDEE0;">
                                    <span style="color: #155BD4;" @click.stop="editFile">编辑</span>|<span style="color: #155BD4;"
                                        @click.stop="deletFile">删除</span>
                                </div>
                            </div>
                            <div v-else>
                                <span style="display: flex; align-items: center;justify-content: center;">
                                    <img class="uploadIcon" src="@/assets/imgs/icon_upload@2x.png" />
                                    <span class="videoTitle">批量上传</span>
                                </span>
                                <span>单个文件不超过 15M</span>
                            </div>
    
                        </el-upload>
    
    
    
    
    
    uploadAvatarFile(param) {
                console.log('param-----' + param.file);
                const formData = new FormData();
                formData.append("file", param.file);
                if (this.form.id) {
                    formData.append("id", this.form.id);
                }
    
                uploadExcel(formData).then((res) => {
                    this.form.action = eval(res.data);
                    this.$refs.dynamicValidateForm.clearValidate("action");
                    this.$refs.uploadFile.clearFiles();
                });
            },
    
            beforeAvatarUpload(rawFile) {
                const isPng = rawFile.type === "application/vnd.ms-excel" || rawFile.type === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    
                if (!isPng)  {
                    ElMessage.warning('文件格式只支持xls或xlsx')
                    return false
                }  
    
                if (rawFile.size / 1024 / 1024 > 14) {
                    ElMessage.error('单个文件不能超过15MB!')
                    return false
                }
                return true
            },
    

    相关文章

      网友评论

          本文标题:vue -el-upload上传文件的几种方式

          本文链接:https://www.haomeiwen.com/subject/cbxcidtx.html