国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

vue el-upload實(shí)現(xiàn)圖片和文字上傳

這篇具有很好參考價(jià)值的文章主要介紹了vue el-upload實(shí)現(xiàn)圖片和文字上傳。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

一、需求:

在表單中使用圖片上傳,每一張上傳的圖片都可以加上文字說明通過表單一起傳到后臺(tái),最后再其他需要的地方展示出來。

el-upload v-model,js,vue,前端,vue.js,elementui,前端

?二、實(shí)現(xiàn):

  1. 后端表單提交時(shí),圖片需要的格式是:imageList[ {?fileUrl:' ',?imageExplain:' '?} ]
  2. 界面代碼
  • v-model="form.imageUrl"

  • :action="fileUrl" 調(diào)用接口自定義的fileUrl上傳圖片到的服務(wù)路徑

  • :on-success="handleSuccess"? 成功時(shí)回調(diào)方法

  • :before-upload="beforeAvatarUpload" 上傳時(shí)的校驗(yàn)回調(diào)

  • :before-remove="() => false"? 防止按下刪除鍵圖片和文字被刪除

  • :file-list="imageList"? 圖片都存到的自定義 imageList 數(shù)組

  • slot="file" slot-scope="{ file }" 自定義插槽,可以在里面實(shí)現(xiàn)輸入框

<el-form-item
        label="界面:"
        class="uploader-item"
      >
        <el-upload
          v-model="form.imageUrl"
          :action="fileUrl"
          accept="image/jpg,image/jpeg,image/png"
          list-type="picture-card"
          :limit="9"
          :on-success="handleSuccess"
          :before-upload="beforeAvatarUpload"
          :before-remove="() => false"
          :file-list="imageList"
        >
          <i slot="default" class="el-icon-plus"></i>
          <div slot="file" slot-scope="{ file }">
            <img
              class="el-upload-list__item-thumbnail"
              :src="file.fileUrl"
              alt=""
            />
            <el-input
              v-model="file.imageExplain"
              placeholder="圖片說明"
              clearable
            >
            </el-input>
            <span class="el-upload-list__item-actions">
              <span
                class="el-upload-list__item-preview"
                @click="handlePictureCardPreview(file)"
              >
                <i class="el-icon-zoom-in"></i>
              </span>
              <span
                class="el-upload-list__item-delete"
                @click="handleRemove(file, imageList)"
              >
                <i class="el-icon-delete"></i>
              </span>
            </span>
          </div>
        </el-upload>
        //預(yù)覽的圖片彈框
        <el-dialog
          class="review-dialog"
          append-to-body
          :visible.sync="imgDialogVisible"
        >
          <img width="100%" :src="dialogImageUrl" alt="" />
        </el-dialog>
      </el-form-item>

?3. 參數(shù)定義

 data() {
    return {
      form: {
        imageList: []
      },
      fileUrl: open_upload_path, //圖片文件上傳地址
      imageList: [], //上傳的文件列表,
      dialogImageUrl: '', //圖片預(yù)覽路徑
      imgDialogVisible: false, //預(yù)覽是否可見
    }
  },

4.方法實(shí)現(xiàn)

//(文件上傳成功時(shí)的鉤子)
    handleSuccess(res, file, fileList) {
      if (res.code == 200 && res.result.url) {
        this.imageList.push({
          fileUrl: file.url,   //圖片渲染路徑
          imageUrl: res.result.url,  //要上傳的路徑
          imageExplain: ''  //要上傳的圖片說明
        })
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = ['image/jpg', 'image/jpeg', 'image/png'].includes(file.type)
      const isLt5M = file.size / 1024 / 1024 < 5
      if (!isJPG) {
        this.$message.error('上傳頭像圖片只能是 JPG/PNG 格式!')
      }
      if (!isLt5M) {
        this.$message.error('上傳頭像圖片大小不能超過 5MB!')
      }
      return isJPG && isLt5M
    },
    //刪除
    handleRemove(file, fileList) {
      this.imageList = fileList.filter((item) => {
        return item.uid !== file.uid
      })
    },
    //預(yù)覽
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.fileUrl
      this.imgDialogVisible = true
    }

5. 樣式設(shè)置文章來源地址http://www.zghlxwxcb.cn/news/detail-693038.html

.uploader-item {
  margin: 60px 0 60px;
  :deep(.el-upload-list) {
    .is-success {
      overflow: hidden;
      height: auto;
      width: 192px;
      border: 0;
      border-radius: 0;
      margin-right: 10px;

      img,
      .el-upload-list__item-actions {
        height: 108px;
      }
      .el-input {
        .el-input__inner {
          height: 32px;
        }
      }
    }
    .is-ready,.is-uploading {
      display: none;
    }
  }
  :deep(.el-upload) {
    width: 192px;
    height: 108px;
    line-height: 108px;
    background: transparent;
    border: 1px solid #dcdfe6;
    border-radius: 0;
    margin-bottom: 20px;
    .el-icon-plus {
      color: #dcdfe6;
      font-size: 24px;
    }
  }
}

到了這里,關(guān)于vue el-upload實(shí)現(xiàn)圖片和文字上傳的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包