欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue如何實(shí)現(xiàn)文件預(yù)覽和下載功能的前端上傳組件

 更新時間:2024年09月29日 10:27:51   作者:和燁  
在Vue.js項(xiàng)目中,使用ElementUI的el-upload組件可以輕松實(shí)現(xiàn)文件上傳功能,通過配置組件參數(shù)和實(shí)現(xiàn)相應(yīng)的方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

在前端開發(fā)中,文件上傳和預(yù)覽是常見的功能需求之一。本文將介紹如何利用 Vue.js 結(jié)合 Element UI 的上傳組件(el-upload)實(shí)現(xiàn)文件上傳,并根據(jù)文件類型進(jìn)行預(yù)覽或下載的功能。

1.準(zhǔn)備工作

首先,確保你的項(xiàng)目中已經(jīng)引入了 Vue.js 和 Element UI。在這個示例中,我們使用了 el-upload 組件來管理文件上傳。

1.1 創(chuàng)建 Vue 組件

在 Vue 組件中,我們需要實(shí)現(xiàn)以下功能:

  • 文件上傳功能
  • 文件預(yù)覽功能(針對圖片類型)
  • 文件下載功能(對于其他類型的文件)
<template>
  <div>
    <el-upload
      v-model:file-list="fileList"
      action="你的上傳地址"
      :on-success="handleFileSuccess"
      :on-remove="handleFileRemove"
      :on-error="handleFileError"
      :limit="10"
      :data="fileFormData"
      name="files"
      :on-preview="openFile"
    >
      <el-button type="primary">點(diǎn)擊上傳</el-button>
    </el-upload>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      fileList: [], // 上傳文件列表
      fileFormData: {}, // 額外的上傳參數(shù),如果需要的話
      imageExtensions: ["jpg", "jpeg", "png", "gif", "bmp"], // 圖片格式后綴
    };
  },
  methods: {
    async openFile(file) {
      try {
        const response = await axios.get(`/bbjApi/system/systemfile/${file.id}`, {
          responseType: "blob", // 設(shè)置響應(yīng)類型為 blob
        });
        const blob = new Blob([response.data], {
          type: response.headers["content-type"],
        });
        const url = window.URL.createObjectURL(blob);

        // 根據(jù)文件類型設(shè)置預(yù)覽方式
        const lowerCaseInput = file.name.toLowerCase();
        if (this.imageExtensions.some((ext) => lowerCaseInput.endsWith("." + ext))) {
          // 如果是圖片類型,創(chuàng)建彈窗進(jìn)行預(yù)覽
          this.createImageModal(url);
        } else {
          // 其他類型的文件直接下載
          this.downloadFile(url, file.name);
          window.URL.revokeObjectURL(url); // 釋放對象 URL
        }

      } catch (error) {
        this.$message.error("打開文件異常,請聯(lián)系管理員");
      }
    },
    createImageModal(url) {
      // 創(chuàng)建彈窗容器
      const modalContainer = document.createElement("div");
      modalContainer.style.position = "fixed";
      modalContainer.style.top = "0";
      modalContainer.style.left = "0";
      modalContainer.style.width = "100%";
      modalContainer.style.height = "100%";
      modalContainer.style.backgroundColor = "rgba(0, 0, 0, 0.7)";
      modalContainer.style.zIndex = "9999";
      modalContainer.style.display = "flex";
      modalContainer.style.justifyContent = "center";
      modalContainer.style.alignItems = "center";

      // 創(chuàng)建圖片元素
      const imgElement = document.createElement("img");
      imgElement.src = url;
      imgElement.style.maxWidth = "80%";
      imgElement.style.maxHeight = "80%";

      // 創(chuàng)建關(guān)閉按鈕
      const closeButton = document.createElement("button");
      closeButton.textContent = "關(guān)閉";
      closeButton.style.position = "absolute";
      closeButton.style.top = "10px";
      closeButton.style.right = "10px";
      closeButton.style.padding = "5px 10px";
      closeButton.style.backgroundColor = "#409EFF";
      closeButton.style.border = "none";
      closeButton.style.cursor = "pointer";
      closeButton.style.borderRadius = "10px";
      closeButton.style.color = "#fff";

      // 點(diǎn)擊關(guān)閉按鈕時移除彈窗
      closeButton.addEventListener("click", () => {
        document.body.removeChild(modalContainer);
        window.URL.revokeObjectURL(url); // 釋放對象 URL
      });

      // 將圖片和關(guān)閉按鈕添加到彈窗容器中
      modalContainer.appendChild(imgElement);
      modalContainer.appendChild(closeButton);

      // 將彈窗容器添加到頁面主體中
      document.body.appendChild(modalContainer);
    },
    downloadFile(url, fileName) {
      // 創(chuàng)建下載鏈接
      const link = document.createElement("a");
      link.href = url;
      link.setAttribute("download", fileName);
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    },
    handleFileSuccess(response, file, fileList) {
      // 處理文件上傳成功的回調(diào)
      console.log("文件上傳成功", response);
    },
    handleFileRemove(file, fileList) {
      // 處理文件移除的回調(diào)
      console.log("文件移除", file);
    },
    handleFileError(error, file, fileList) {
      // 處理文件上傳失敗的回調(diào)
      console.error("文件上傳失敗", error);
    },
  },
};
</script>

<style>
/* 可以根據(jù)需要添加樣式 */
</style>

1.2 組件說明

  • el-upload 組件配置:配置了文件上傳的基本參數(shù),如上傳地址、成功、移除和失敗的回調(diào)函數(shù)等。
  • openFile 方法:異步方法,根據(jù)文件類型進(jìn)行預(yù)覽或下載。如果是圖片類型,則創(chuàng)建一個模態(tài)框顯示圖片;否則,直接下載文件。
  • createImageModal 方法:創(chuàng)建圖片預(yù)覽的模態(tài)框,包括顯示圖片和關(guān)閉按鈕。
  • downloadFile 方法:創(chuàng)建下載鏈接并進(jìn)行下載。

2.注意事項(xiàng)

  • Blob 對象:用于處理從服務(wù)器獲取的二進(jìn)制數(shù)據(jù),如圖片內(nèi)容。
  • 文件類型判斷:通過文件后綴名判斷文件類型,這里示例了圖片類型的判斷方式。

通過以上方法,你可以在 Vue.js 項(xiàng)目中利用 Element UI 的 el-upload 組件實(shí)現(xiàn)文件上傳并根據(jù)文件類型進(jìn)行預(yù)覽或下載的功能。這樣的實(shí)現(xiàn)不僅提升了用戶體驗(yàn),還增加了系統(tǒng)的交互性和可用性。

總結(jié)

到此這篇關(guān)于Vue如何實(shí)現(xiàn)文件預(yù)覽和下載功能的前端上傳組件的文章就介紹到這了,更多相關(guān)Vue文件預(yù)覽和下載功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 如何在vue中使用jointjs過程解析

    如何在vue中使用jointjs過程解析

    這篇文章主要介紹了如何在vue中使用jointjs過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • webstorm+vue初始化項(xiàng)目的方法

    webstorm+vue初始化項(xiàng)目的方法

    今天小編就為大家分享一篇webstorm+vue初始化項(xiàng)目的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • vue3中如何使用ts

    vue3中如何使用ts

    這篇文章主要介紹了vue3中如何使用ts,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • vue實(shí)現(xiàn)點(diǎn)擊復(fù)制到粘貼板

    vue實(shí)現(xiàn)點(diǎn)擊復(fù)制到粘貼板

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)點(diǎn)擊復(fù)制到粘貼板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • 查找Vue中下標(biāo)的操作(some和findindex)

    查找Vue中下標(biāo)的操作(some和findindex)

    這篇文章主要介紹了查找Vue中下標(biāo)的操作(some和findindex),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Vuex(多組件數(shù)據(jù)共享的Vue插件)搭建與使用

    Vuex(多組件數(shù)據(jù)共享的Vue插件)搭建與使用

    Vuex是實(shí)現(xiàn)組件全局狀態(tài)(數(shù)據(jù))管理的一種機(jī)制,可以方便的實(shí)現(xiàn)組件之間數(shù)據(jù)的共享,數(shù)據(jù)緩存等等,下面這篇文章主要給大家介紹了關(guān)于Vuex(多組件數(shù)據(jù)共享的Vue插件)搭建與使用的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • vue中ref引用操作DOM元素的實(shí)現(xiàn)

    vue中ref引用操作DOM元素的實(shí)現(xiàn)

    本文主要介紹了vue中ref引用操作DOM元素的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • 對vuex中g(shù)etters計(jì)算過濾操作詳解

    對vuex中g(shù)etters計(jì)算過濾操作詳解

    今天小編就為大家分享一篇對vuex中g(shù)etters計(jì)算過濾操作詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • vue渲染方式render和template的區(qū)別

    vue渲染方式render和template的區(qū)別

    這篇文章主要介紹了vue渲染方式render和template的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 淺談vue中computed屬性對data屬性賦值為undefined的原因

    淺談vue中computed屬性對data屬性賦值為undefined的原因

    本文主要介紹了淺談vue中computed屬性對data屬性賦值為undefined的原因,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02

最新評論