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

Vue - 使用Element UI Upload / importExcelJs進行文件導入

這篇具有很好參考價值的文章主要介紹了Vue - 使用Element UI Upload / importExcelJs進行文件導入。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

1 情景一

需求背景后端配合,點擊"導入"按鈕,彈出“導入”彈窗,將電腦本地Excel表格數(shù)據(jù)導入到頁面中表格位置(需要調(diào)用后端接口),而頁面中表格通過后端接口獲取最新數(shù)據(jù)。
實現(xiàn)思路:彈窗嵌入 Element UI Upload 上傳組件,獲取到文件后調(diào)后端接口。

action: 上傳的地址
file-list: 上傳的文件列表, 例如: [{name: ‘food.jpg’, url: ‘https://xxx.cdn.com/xxx.jpg’}]
headers: 設(shè)置上傳的請求頭部(上傳的文件可能是有固定格式的(表頭),這個是在后端設(shè)定好了的,所以如果上傳的文件格式不對可能會引起報錯。)
before-upload: 上傳文件之前的鉤子,可作上傳之前校驗
on-error: 文件上傳失敗時的鉤子
on-exceed: 文件超出個數(shù)限制時的鉤子
on-success: 文件上傳成功時的鉤子
on-change: 文件狀態(tài)改變時的鉤子
this.$refs.fileUpload.submit(): 手動上傳文件列表(fileUpload是自定義的名字)

  • 組件代碼
<template>
  <span>
    <el-button
      plain
      icon="el-icon-upload2"
      type="primary"
      size="mini"
      :disabled="disabled"
      @click="importFile"
    >{{ $t('import') }}</el-button>
    <el-dialog
      v-if="open"
      :title="importTitle"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
      :show-close="false"
      @close="cancel"
    >
      <div v-if="isShowRecord" class="record-btn">
        <el-button icon="el-icon-time" type="primary" size="mini" @click="recordBtnClick">
          <span>{{ recordText }}</span>
        </el-button>
      </div>
      <div style="text-align: center;">
        <el-upload
          ref="fileUpload"
          multiple
          :auto-upload="false"
          :action="uploadFileUrl"
          :before-upload="handleBeforeUpload"
          :file-list="fileList"
          :limit="limit"
          :on-error="handleUploadError"
          :on-exceed="handleExceed"
          :on-success="handleUploadSuccess"
          :on-change="handleChange"
          :headers="headers"
          class="upload-file-uploader"
        >
          <div style="border:1px dashed #e9e9e9">
            <i class="el-icon-plus avatar-uploader-icon" style="margin:80px;font-size: 25px;" />
          </div>
          <!-- 上傳提示 -->
          <div v-if="showTip" slot="tip" class="el-upload__tip">
            {{ $t("pleaseUpload") }}
            <template v-if="fileSize">
              {{ $t("sizeLess") }}
              <b style="color: #f56c6c">{{ fileSize }}MB</b>
            </template>
            <template v-if="fileType">
              {{ $t("formatIs") }}
              <b style="color: #f56c6c">{{ fileType.join("/") }}</b>
            </template>
            {{ $t("file") }}
          </div>
        </el-upload>
      </div>
      <div class="template">
        <!-- <form v-show="false" id="templateForm" :action="templateUrl" method="get" /> -->
        <span @click="downloadFile">{{ $t("downloadThisTemplate") }}</span>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t('confirm') }}</el-button>
        <el-button @click="cancel">{{ $t('cancel') }}</el-button>
      </div>
    </el-dialog>
  </span>
</template>
<script>
import { getToken } from '@/utils/auth'
import { downloadGet } from '@/utils/request'
import moment from 'moment'
import $ from '@/i18n/index'

export default {
  name: 'FileImport',
  props: {
    // 是否禁用導入按鈕
    disabled: {
      type: Boolean,
      default: false
    },
    // 導入彈窗標題
    importTitle: {
      type: String,
      default: () => {
        return $.t('fileImport')
      }
    },
    // 數(shù)量限制
    limit: {
      type: Number,
      default: 1
    },
    // 大小限制(MB)
    fileSize: {
      type: Number,
      default: 20
    },
    // 文件類型, 例如['png', 'jpg', 'jpeg']
    fileType: {
      type: Array,
      default: () => ['csv']
    },
    // 是否顯示提示
    isShowTip: {
      type: Boolean,
      default: true
    },
    // 下載模板路徑
    downLoadUrl: { type: String, default: null },
    // 下載的全拼路徑
    fullPathUrl: { type: String, default: null },
    // 模板名稱
    fileName: { type: String, default: moment().format('yyyyMMDDHHmmss') },
    // 導入數(shù)據(jù)路徑
    importUrl: { type: String, default: null },
    // 是否展示導入導出記錄按鈕
    isShowRecord: {
      type: Boolean,
      default: false
    },
    // 導入導出記錄文字
    recordText: {
      type: String,
      default: () => {
        return $.t('importAndExportRecord')
      }
    }
  },
  data() {
    return {
      open: false,
      number: 0,
      baseUrl: process.env.VUE_APP_BASE_API,
      // templateUrl:
      //   this.fullPathUrl || process.env.VUE_APP_BASE_API + this.downLoadUrl,
      fileList: []
    }
  },
  computed: {
    headers() {
      return { Authorization: getToken() }
    },

    // 是否顯示提示
    showTip() {
      return this.isShowTip && (this.fileType || this.fileSize)
    },

    // 上傳文件服務(wù)器地址
    uploadFileUrl() {
      return process.env.VUE_APP_BASE_API + this.importUrl
    }

    // templateUrl() {
    //   return this.fullPathUrl || process.env.VUE_APP_BASE_API + this.downLoadUrl
    // }
  },
  methods: {
    importFile() {
      this.open = true
    },
    submitForm() {
      if (this.fileList.length === 0) {
        this.$modal.msgError(this.$t('pleaseImportFile'))
        return
      }
      this.$refs.fileUpload.submit()
    },
    cancel() {
      this.$refs.fileUpload.clearFiles()
      this.open = false
      this.$emit('importSuccess')
    },
    // 上傳前校檢格式和大小
    handleBeforeUpload(file) {
      // 校檢文件類型
      if (this.fileType) {
        const fileName = file.name.split('.')
        const fileExt = fileName[fileName.length - 1]
        const isTypeOk = this.fileType.indexOf(fileExt) >= 0
        if (!isTypeOk) {
          this.$modal.msgError(
            `${this.$t('fileTypeErrorUpload')}${this.fileType.join(
              '/'
            )}${this.$t('file')}`
          )
          return false
        }
      }
      // 校檢文件大小
      if (this.fileSize) {
        const isLt = file.size / 1024 / 1024 < this.fileSize
        if (!isLt) {
          this.$modal.msgError(
            `${this.$t('uploadFileSizeLess')} ${this.fileSize} MB!`
          )
          return false
        }
      }
      this.$modal.loading(this.$t('waitForUpload'))
      this.number++
      return true
    },
    // 文件個數(shù)超出
    handleExceed() {
      this.$modal.msgError(`${this.$t('uploadFileNumberLess')} ${this.limit}`)
    },
    // 上傳失敗
    handleUploadError() {
      this.$modal.msgError(this.$t('uploadFail'))
      this.$modal.closeLoading()
    },
    // 上傳成功回調(diào)
    handleUploadSuccess(res, file) {
      if (res.code === 0) {
        this.$message({ type: 'success', message: this.$t('uploadSuccess') })
        this.cancel()
        this.$emit('handleUploadSuccess')
      } else {
        this.number--
        this.$modal.msgError(res.msg)
        this.$refs.fileUpload.handleRemove(file)
      }
      this.$modal.closeLoading()
    },
    downloadFile() {
      // document.getElementById('templateForm').submit()
      let dowloadUrl = this.downLoadUrl
      if (this.fullPathUrl) {
        dowloadUrl = this.fullPathUrl
      }
      downloadGet(dowloadUrl, `${this.fileName}.${this.fileType}`, {})
    },
    recordBtnClick() {
      this.$emit('recordBtnClick')
    },
    handleChange(file, fileList) {
      this.fileList = fileList
    }
  }
}
</script>
<style lang="scss" scoped>
.template {
  text-align: center;
  color: #1890ff;
  padding: 10px;
  span:hover {
    cursor: pointer;
    border-bottom: 1px solid #1890ff;
  }
}

.record-btn {
  position: absolute;
  right: 18px;
  top: 18px;
}
</style>
// 通用下載方法,blob形式
export function downloadGet(url, filename, config) {
  downloadLoadingInstance = Loading.service({
    text: '正在下載數(shù)據(jù),請稍候',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  })
  return service
    .get(url, {
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      responseType: 'blob',
      ...config
    })
    .then(async(data) => {
      const isLogin = await blobValidate(data)
      if (isLogin) {
        const blob = new Blob([data])
        saveAs(blob, filename)
      } else {
        const resText = await data.text()
        const rspObj = JSON.parse(resText)
        const errMsg =
          errorCode[rspObj.code] || rspObj.msg || errorCode['default']
        Message.error(errMsg)
      }
      downloadLoadingInstance.close()
    })
    .catch((r) => {
      console.error(r)
      Message.error('下載文件出現(xiàn)錯誤,請聯(lián)系管理員!')
      downloadLoadingInstance.close()
    })
}
import Cookies from 'js-cookie'

const TokenKey = 'Admin-Token'

export function getToken() {
  return Cookies.get(TokenKey)
}

Vue - 使用Element UI Upload / importExcelJs進行文件導入,vue,前端,vue.js

  • 使用組件
 <FileImport
      down-load-url="/client/template/export_template/sec_type"
      import-url="/client/sec_type/import"
      file-name="導入持倉"
/>

Vue - 使用Element UI Upload / importExcelJs進行文件導入,vue,前端,vue.js

2 情景二

需求背景純前端實現(xiàn)文件導入的情景。例如,一個常規(guī)excel文件填寫模板,在用戶的電腦上,用戶上傳完后,還可以在預覽展示時,在線修改,改完可以下載,也可以將數(shù)據(jù)給到服務(wù)端,但這時,比如這模板數(shù)據(jù)通常不多,比如是一個團隊成員這樣的數(shù)據(jù),通過文件流的形式傳給后端,可能不是很理想,倒不如前端解析傳那幾行數(shù)據(jù)就行。
實現(xiàn)思路importExcel.js 導入excel文件文章來源地址http://www.zghlxwxcb.cn/news/detail-769552.html

  1. 使用html支持上傳標簽從本地獲取文件,例如type為file的input,el-upload等。
  2. 利用FileReader將文件讀取為二進制字符串。
  3. 使用XLSX插件XLSX.read()方法,將二進制字符串轉(zhuǎn)換成excel文件的工作蒲對象workbook(簡寫成wb)。
  4. 通過XLSX.utils.sheet_to_json()方法,從wb中獲取第一張 Sheets表格數(shù)據(jù)并將其轉(zhuǎn)換為json數(shù)據(jù)。
  5. 重組json數(shù)據(jù)生成數(shù)組,即是根據(jù)自己的定義的列字段名,重新組成符合自己需求的json數(shù)據(jù)。因為從excel中提取的數(shù)據(jù)是沒有字段名或字段名不符合要求的,而我們需要渲染在頁面表格中又確實需要合適的字段名。
/* eslint-disable */
/* 導入excel文件 */
/**
 * @param file 文件流
 * @param tableTemplate 要導入的表格模板,一個數(shù)組,如:
 * tableTemplate: ['userCode', 'userName', 'department', 'major', 'position'],其中的值
 * 為表格的字段名,注意字段的順序應(yīng)與實際的導入excel一致。
 */
export default function importExcel (file, tableTemplate) {
  return new Promise((resolve, reject) => {
    let f = file.raw // 獲取文件內(nèi)容
    // 通過DOM取文件數(shù)據(jù)
    let rABS = false // 是否將文件讀取為二進制字符串
    let reader = new FileReader()
    FileReader.prototype.readAsBinaryString = function (f) {
      let binary = ''
      let rABS = false // 是否將文件讀取為二進制字符串
      let wb // 讀取完成的數(shù)據(jù)
      let outdata
      let reader = new FileReader()
      reader.onload = function (e) {
        let bytes = new Uint8Array(reader.result)
        let length = bytes.byteLength
        for (let i = 0; i < length; i++) {
          binary += String.fromCharCode(bytes[i])
        }
        let XLSX = require('xlsx')
        if (rABS) {
          wb = XLSX.read(btoa(binary), { // 手動轉(zhuǎn)化
            type: 'base64'
          })
        } else {
          wb = XLSX.read(binary, {
            type: 'binary'
          })
        }
        outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]) // outdata就是表格中的值

        let arr = []
        // 下面是數(shù)據(jù)解析提取邏輯
        if (tableTemplate.length > 0) {
          let tempArr = Object.keys(outdata[0])
          let tempArrNew = []
          for (let i in tempArr) {
            for (let k in tableTemplate) {
              if (i === k) {
                tempArrNew.push({fieldE: tableTemplate[k], fieldC: tempArr[i]})
              }
            }
          }

          tempArr = tempArrNew
          outdata.map(item => {
            let obj = {}
            tempArr.map(temp2 => {
              obj[temp2.fieldE] = item[temp2.fieldC]
            })
            arr.push(obj)
          })
        }

        resolve(arr)
      }
      reader.readAsArrayBuffer(f)
    }

    if (rABS) {
      reader.readAsArrayBuffer(f)
    } else {
      reader.readAsBinaryString(f)
    }
  })
}

到了這里,關(guān)于Vue - 使用Element UI Upload / importExcelJs進行文件導入的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

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

相關(guān)文章

  • Element UI | Upload 使用submit方法手動上傳文件

    點擊上傳文件按鈕不立刻進行上傳,而是點擊提交后再調(diào)用上傳接口,和表單一起提交。 js:? 判斷文件沒有上傳即先進行文件上傳,上傳文件成功后在調(diào)用表單上傳接口,否則直接調(diào)用接口上傳表單。submit為element ui內(nèi)部方法。表單上傳接口一般傳遞文件id。

    2024年02月15日
    瀏覽(28)
  • vue 2.0+element ui 使用el-upload圖片上傳

    vue 2.0+element ui 使用el-upload圖片上傳

    ** ** 使用el-upload將圖片加載成Base64格式,通過form統(tǒng)一上傳給后端 1、創(chuàng)建通用component ImgComponent.vue 2、在父組件中使用 3、最后通過form統(tǒng)一submit到后端,圖片參數(shù)傳base64即可。 ps:起初在數(shù)據(jù)庫中,將存圖片的字段類型設(shè)置為BLOB,以二進制的形勢存儲,后面發(fā)現(xiàn)會將“:”轉(zhuǎn)

    2024年02月12日
    瀏覽(97)
  • vue前端直接使用element-ui的upload組件上傳到阿里云OSS存儲

    vue前端直接使用element-ui的upload組件上傳到阿里云OSS存儲

    因為公司的服務(wù)器比較拉吧,所以老大決定將數(shù)據(jù)文件等上傳到阿里云服務(wù)器,通過ali-oss方式,這樣的話讀取文件也比較快? (能看到這說明什么安裝element-ui都會了,我就不詳細介紹了,直接跳過)? 在執(zhí)行下面操作之前,你要確定你的阿里云能看到上邊這個頁面? ?這里主

    2024年02月10日
    瀏覽(37)
  • vue3 - 使用element-plus組件庫el-upload上傳超大mp4視頻分片上傳,Upload上傳大文件mp4視頻進行切片分段上傳到后端服務(wù)器教程,vue3如何上傳很大的視頻(詳細示例代碼

    vue3 - 使用element-plus組件庫el-upload上傳超大mp4視頻分片上傳,Upload上傳大文件mp4視頻進行切片分段上傳到后端服務(wù)器教程,vue3如何上傳很大的視頻(詳細示例代碼

    在vue3+elementPlus中,使用el-upload組件\\\"切片分段\\\"上傳mp4大視頻到服務(wù)器,支持任意大視頻、大文檔、大壓縮包等超大文件,通用方法將其拆分成多個小段進行逐個逐條上傳到后端(支持斷點續(xù)傳、下載預覽)。 詳細大文件分片功能源碼,可只拿前端源碼或只拿springboot(Java)后

    2024年03月16日
    瀏覽(41)
  • Element-UI的Upload 上傳文件

    Element-UI的Upload 上傳文件

    \\\"element-ui\\\": \\\"^2.15.9\\\" Upload 上傳官方文檔 2.1 自動上傳 限制一次只能上傳一個文件,并判斷上傳的文件大小及文件類型; 自動上傳:即選擇文件后即開始校驗,校驗通過后調(diào)接口上傳到服務(wù)器 2.2 手動上傳 限制一次只能上傳一個文件,并判斷上傳的文件大小及文件類型; 手動上

    2024年02月11日
    瀏覽(22)
  • Element-ui upload 手動上傳文件

    (隨手一發(fā),可以拿過來就用,不做解釋看不懂的可以照搬,??代碼可復制??) ? ?

    2024年02月11日
    瀏覽(25)
  • vue Element ui上傳組件el-upload封裝

    注釋: 1. limit 可上傳圖片數(shù)量 2. lableName 當前組件name,用于一個頁面多次使用上傳組件,對數(shù)據(jù)進行區(qū)分 3. upload 上傳圖片發(fā)生變化觸發(fā),返回已上傳圖片的信息 4. imgUrl 默認圖片

    2024年02月11日
    瀏覽(30)
  • element-ui文件上傳el-upload

    element-ui文件上傳el-upload

    ????????element-ui官網(wǎng)中有文件上傳 ????????首先先展示一下我頁面的實現(xiàn)效果,如下圖所示: ?????????從圖中可以看出,我這邊實現(xiàn)的是一個可顯示進度條的文件上傳操作,而且如果上傳的文件很大等,還可以中斷文件上傳。 ??????? 值得注意的是,如果有添

    2024年02月05日
    瀏覽(28)
  • Element UI Plus + Vue3 給 Upload設(shè)置請求頭

    問題描述 在vue項目中我們發(fā)送 ajax 請求一般都會使用 axios,并在 axios 中設(shè)置axios.defaults.baseURL,請求的基本地址,并在請求攔截器中設(shè)置 headers 使用 el-upload 上傳組件時,action 為必選參數(shù),上傳的地址。 但此時我們?yōu)閍ction填寫地址不能不寫基本地址而僅寫 upload,要寫完整的

    2024年02月21日
    瀏覽(23)
  • element ui ---- el-upload實現(xiàn)手動上傳多個文件

    ui部分 js 實現(xiàn)上傳

    2024年02月15日
    瀏覽(28)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包