亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue el-upload單圖片上傳功能實(shí)現(xiàn)

 更新時(shí)間:2023年11月25日 10:42:15   作者:愛學(xué)習(xí)的瘋傾  
這篇文章主要介紹了Vue el-upload單圖片上傳功能實(shí)現(xiàn),本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、前端相關(guān):

<!--:action里面放圖片上傳調(diào)取的后臺方法 :headers設(shè)置上傳的請求頭部,使請求攜帶自定義token,獲取訪問權(quán)限 -->
<!--:on-success圖片上傳成功后的回調(diào)方法,用于拿到圖片存儲路徑等信息-->
<!--:before-upload圖片上傳前的邏輯判斷,例如判斷圖片大小,格式等-->
<!--:on-preview圖片預(yù)覽方法 :on-remove圖片刪除方法 list-type代表文件列表的類型 -->
<!--file-list存放成功上傳圖片列表,這里此屬性用于修改功能時(shí)頁面已有圖片的顯示-->
<el-form-item label="預(yù)覽縮略圖" prop="articleImg" label-width="40">
              <el-upload
                :action="imgUpload.url"
                :headers="imgUpload.headers"
                list-type="picture-card"
                :limit="limit"
                :on-exceed="handleExceed"
                :on-success="handlePictureSuccess"
                :before-upload="beforeAvatarUpload"
                :on-preview="handlePictureCardPreview"
                :file-list="fileList"
              >
                <i class="el-icon-plus"></i>
              </el-upload>
              <el-dialog :visible.sync="dialogVisible">
                <img width="100%" v-if="imageUrl" :src="imageUrl" alt="">
              </el-dialog>
            </el-form-item>

二、屬性值方法的定義: 

export default {
  name: "Article",
  data() {
    return {
    // 圖片數(shù)量限制
    limit: 1,
    //頁面上存的暫時(shí)圖片地址List
      fileList: [{url: ""}],
    //圖片地址
      imageUrl: "",
      dialogVisible: false,
      imgUpload: {
          // 設(shè)置上傳的請求頭部
          headers: {
            Authorization: "Bearer " + getToken()
          },
          // 圖片上傳的方法地址:
          url: process.env.VUE_APP_BASE_API + "/forum/forum/multiPicturesUpload",
      }
    };
},
methods: {
// 表單重置
 reset()
{  ...... 忽略其它
  this.fileList = undefined;  this.resetForm("form");
}
/** 修改按鈕操作 */
handleUpdate(row) {
  this.reset();
  this.getTreeSelect();
  const articleId = row.articleId || this.ids;
  getArticle(articleId).then(response => {
    this.fileList = [{ url: process.env.VUE_APP_BASE_API + response.data.articleImg}]   this.form = response.data;
    this.open = true;
    this.title = "修改文章";
  });
},
/** 提交按鈕 */
submitForm: function() {
  this.form.articleImg = this.imageUrl; // 注:重要(用于添加到數(shù)據(jù)庫)
  this.$refs["form"].validate(valid => {
    if (valid) {
      if (this.form.articleId != undefined) {
        updateArticle(this.form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      } else {
        addArticle(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      }
    }
  });
},
//圖片上傳前的相關(guān)判斷
beforeAvatarUpload(file) {
  const isJPG = file.type === 'image/jpeg' || file.type == 'image/png';
  const isLt2M = file.size / 1024 / 1024 < 5;
  if (!isJPG) {
    this.$message.error('上傳頭像圖片只能是 JPG/PNG 格式!');
  }
  if (!isLt2M) {
    this.$message.error('上傳頭像圖片大小不能超過 5MB!');
  }
  return isJPG && isLt2M;
},
//圖片預(yù)覽
handlePictureCardPreview(file) {
  this.imageUrl = file.url;
  this.dialogVisible = true;
},
//圖片上傳成功后的回調(diào)
handlePictureSuccess(res, file) {
  //設(shè)置圖片訪問路徑 (articleImg 后臺傳過來的的上傳地址)
  this.imageUrl = file.response.articleImg;
},
// 文件個(gè)數(shù)超出
handleExceed() {
  this.$modal.msgError(`上傳鏈接LOGO圖片數(shù)量不能超過 ${this.limit} 個(gè)!`);
},
}
};
export default {
  name: "Article",
  data() {
    return {
    // 圖片數(shù)量限制
    limit: 1,
    //頁面上存的暫時(shí)圖片地址List
      fileList: [{url: ""}],
    //圖片地址
      imageUrl: "",
      dialogVisible: false,
      imgUpload: {
          // 設(shè)置上傳的請求頭部
          headers: {
            Authorization: "Bearer " + getToken()
          },
          // 圖片上傳的方法地址:
          url: process.env.VUE_APP_BASE_API + "/forum/forum/multiPicturesUpload",
      }
    };
},
methods: {
// 表單重置
 reset()
{  ...... 忽略其它
  this.fileList = undefined;  this.resetForm("form");
}
/** 修改按鈕操作 */
handleUpdate(row) {
  this.reset();
  this.getTreeSelect();
  const articleId = row.articleId || this.ids;
  getArticle(articleId).then(response => {
    this.fileList = [{ url: process.env.VUE_APP_BASE_API + response.data.articleImg}]   this.form = response.data;
    this.open = true;
    this.title = "修改文章";
  });
},
/** 提交按鈕 */
submitForm: function() {
  this.form.articleImg = this.imageUrl; // 注:重要(用于添加到數(shù)據(jù)庫)
  this.$refs["form"].validate(valid => {
    if (valid) {
      if (this.form.articleId != undefined) {
        updateArticle(this.form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      } else {
        addArticle(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      }
    }
  });
},
//圖片上傳前的相關(guān)判斷
beforeAvatarUpload(file) {
  const isJPG = file.type === 'image/jpeg' || file.type == 'image/png';
  const isLt2M = file.size / 1024 / 1024 < 5;
  if (!isJPG) {
    this.$message.error('上傳頭像圖片只能是 JPG/PNG 格式!');
  }
  if (!isLt2M) {
    this.$message.error('上傳頭像圖片大小不能超過 5MB!');
  }
  return isJPG && isLt2M;
},
//圖片預(yù)覽
handlePictureCardPreview(file) {
  this.imageUrl = file.url;
  this.dialogVisible = true;
},
//圖片上傳成功后的回調(diào)
handlePictureSuccess(res, file) {
  //設(shè)置圖片訪問路徑 (articleImg 后臺傳過來的的上傳地址)
  this.imageUrl = file.response.articleImg;
},
// 文件個(gè)數(shù)超出
handleExceed() {
  this.$modal.msgError(`上傳鏈接LOGO圖片數(shù)量不能超過 ${this.limit} 個(gè)!`);
},
}
};

注:在提交事件中添加:this.form.articleImg = this.imageUrl;

三、效果如下:

四、后臺上傳圖片方法代碼:

注:articleImg傳給了前端 handlePictureSuccess回調(diào)方法中

/**
     * 縮略圖上傳
     */
    @Log(title = "預(yù)覽縮略圖", businessType = BusinessType.UPDATE)
    @PostMapping("/articleImg")
    public AjaxResult uploadFile(MultipartFile file) throws IOException
    {
        if (!file.isEmpty())
        {
            String articleImg = FileUploadUtils.upload(RuoYiConfig.getArticleImgPath(), file);
            if (!StringUtils.isEmpty(articleImg))
            {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("articleImg", articleImg);
                return ajax;
            }
        }
        return AjaxResult.error("上傳圖片異常,請聯(lián)系管理員");
    }

1、文件上傳工具類:FileUploadUtils

/**
     * 根據(jù)文件路徑上傳
     *
     * @param baseDir 相對應(yīng)用的基目錄
     * @param file 上傳的文件
     * @return 文件名稱
     * @throws IOException
     */
    public static final String upload(String baseDir, MultipartFile file) throws IOException
    {
        try
        {
            return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
        }
        catch (Exception e)
        {
            throw new IOException(e.getMessage(), e);
        }
    }

2、讀取項(xiàng)目相關(guān)配置:RuoYiConfig

package com.ruoyi.common.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
 * 讀取項(xiàng)目相關(guān)配置
 * 
 * @author ruoyi
 */
@Component
@ConfigurationProperties(prefix = "ruoyi")
public class RuoYiConfig
{/** 上傳路徑 */
    private static String profile;
public static String getProfile()
    {
        return profile;
    }
    public void setProfile(String profile)
    {
        RuoYiConfig.profile = profile;
    }
/**
     * 獲取導(dǎo)入上傳路徑
     */
    public static String getImportPath()
    {
        return getProfile() + "/import";
    }/**
     * 獲取預(yù)覽縮略圖上傳路徑
     * @return
     */
    public static String getArticleImgPath()
    {
        return getProfile() + "/articleImg";
    }
    /**
     * 獲取下載路徑
     */
    public static String getDownloadPath()
    {
        return getProfile() + "/download/";
    }
    /**
     * 獲取上傳路徑
     */
    public static String getUploadPath()
    {
        return getProfile() + "/upload";
    }
}

到此這篇關(guān)于Vue el-upload單圖片上傳的文章就介紹到這了,更多相關(guān)Vue el-upload單圖片上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue中路由跳轉(zhuǎn)的多種方式(和$router下路由跳轉(zhuǎn)的那幾個(gè)方法的區(qū)別)

    vue中路由跳轉(zhuǎn)的多種方式(和$router下路由跳轉(zhuǎn)的那幾個(gè)方法的區(qū)別)

    Vue.js是一款流行的前端JavaScript框架,它提供了多種方式來實(shí)現(xiàn)路由跳轉(zhuǎn),本文給大家分享vue中路由跳轉(zhuǎn)的幾種方式(和$router下路由跳轉(zhuǎn)的那幾個(gè)方法的區(qū)別),感興趣的朋友一起看看吧
    2023-11-11
  • vue3.0+element表格獲取每行數(shù)據(jù)代碼示例

    vue3.0+element表格獲取每行數(shù)據(jù)代碼示例

    這篇文章主要給大家介紹了關(guān)于vue3.0+element表格獲取每行數(shù)據(jù)的相關(guān)資料,在element-ui中,你可以通過為表格的行綁定單擊事件來獲取表格中的一行數(shù)據(jù),需要的朋友可以參考下
    2023-09-09
  • 如何利用vue-cli監(jiān)測webpack打包與啟動時(shí)長

    如何利用vue-cli監(jiān)測webpack打包與啟動時(shí)長

    這篇文章主要給大家介紹了關(guān)于如何利用vue-cli監(jiān)測webpack打包與啟動時(shí)長的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-02-02
  • 詳解vue-cli@2.x項(xiàng)目遷移日志

    詳解vue-cli@2.x項(xiàng)目遷移日志

    這篇文章主要介紹了詳解vue-cli@2.x項(xiàng)目遷移日志,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • vue同一瀏覽器登錄多賬號處理方案

    vue同一瀏覽器登錄多賬號處理方案

    項(xiàng)目在線上會遇到管理員類似權(quán)限比較大的用戶,會在同一瀏覽器登陸多個(gè)賬號,遇到這樣的問題如何處理呢,下面小編給大家介紹vue同一瀏覽器登錄多賬號處理方法,感興趣的朋友一起看看吧
    2024-01-01
  • Vue element-ui父組件控制子組件的表單校驗(yàn)操作

    Vue element-ui父組件控制子組件的表單校驗(yàn)操作

    這篇文章主要介紹了Vue element-ui父組件控制子組件的表單校驗(yàn)操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Vue3使用src動態(tài)引入本地圖片的詳細(xì)步驟

    Vue3使用src動態(tài)引入本地圖片的詳細(xì)步驟

    這篇文章主要給大家介紹了關(guān)于Vue3使用src動態(tài)引入本地圖片的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-12-12
  • Vue中EpicEditor和vue-quill-editor的使用詳解

    Vue中EpicEditor和vue-quill-editor的使用詳解

    EpicEditor和Vue-quill-editor都是基于Quill.js的富文本編輯器,并且都提供了許多強(qiáng)大的功能,下面我們就來介紹一下二者的具體使用,感興趣的小伙伴可以了解一下
    2023-11-11
  • Vue編譯器optimize源碼分析

    Vue編譯器optimize源碼分析

    這篇文章主要為大家介紹了Vue?編譯器optimize源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • vue省市區(qū)三聯(lián)動下拉選擇組件的實(shí)現(xiàn)

    vue省市區(qū)三聯(lián)動下拉選擇組件的實(shí)現(xiàn)

    本篇文章主要介紹了vue省市區(qū)三聯(lián)動下拉選擇組件的相關(guān)知識。具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-04-04

最新評論