最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue3?+?element-plus?的?upload?+?axios?+?django?實(shí)現(xiàn)文件上傳并保存功能

 更新時(shí)間:2024年01月10日 09:02:55   作者:衛(wèi)龍吖  
這篇文章主要介紹了vue3?+?element-plus?的?upload?+?axios?+?django?文件上傳并保存,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

之前在網(wǎng)上搜了好多教程,一直沒(méi)有找到合適自己的,要么只有前端部分沒(méi)有后端,要么就是寫(xiě)的不是很明白。所以還得靠自己摸索出來(lái)后,來(lái)此記錄一下整個(gè)過(guò)程。

  • 其實(shí)就是不要用默認(rèn)的 action,要手動(dòng)實(shí)現(xiàn)上傳方式 http-request,然后再傳給后端進(jìn)行各種操作了
    • 這里隱藏了文件展示列表
    • 展示了上傳文件的個(gè)數(shù)
    • 文件去重上傳
    • 也對(duì)上傳文件的格式做了限制
    • 在點(diǎn)擊創(chuàng)建的時(shí)候 progress 會(huì)隨著上傳進(jìn)度動(dòng)態(tài)變化

環(huán)境安裝什么的就不講了,直接上代碼好吧,這個(gè)是樣式圖

這是vue3代碼

<template>
  <el-upload class="upload-demo form-item" v-model:file-list="fileList" drag multiple :http-request="httpRequest" :show-file-list="false" auto-upload="false" :accept=upload_accept>
      <el-icon class="el-icon--upload"><upload-filled /></el-icon>
      <div class="el-upload__text">拖拽 / 點(diǎn)擊上傳文件 ( zip, jpg, png ……)</div>
      <template #tip>
          <div class="el-upload__tip">已上傳 {{ fileListLength }} 個(gè)文件</div>
      </template>
  </el-upload>
  <el-progress :percentage="progress.curr" :color="progress.color" />
  <el-button type="info" class="btn" @click="removeFile">清空文件</el-button>
  <el-button type="primary" class="btn" @click="create">創(chuàng)建</el-button>
</template>

<script setup lang="ts">
import { ref, watch } from "vue";
import http from "@/utils/axios/index";
import { UploadFilled } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';


const public_elmsg_success = (msg: string) => {
  ElMessage({ type: 'success', duration: 1000, showClose: true, message: msg })
};

const public_elmsg_warning = (msg: string) => {
  ElMessage({ type: 'warning', duration: 1000, showClose: true, message: msg })
};

const public_elmsg_error = (msg: string) => {
  ElMessage({ type: 'error', duration: 1000, showClose: true, message: msg })
};

const upload_accept = ref(".JPG,.PNG,.JPEG,.PCD,.MP4,.AVI,.DAT,.DVR,.VCD,.MOV,.SVCD,.VOB,.DVD,.DVTR,.DVR,.BBC,.EVD,.FLV,.RMVB,.WMV,.MKV,.3GP,.ZIP"); // 限制了上傳文件的格式 大寫(xiě)后綴
const upload_lower = ref(upload_accept.value.split(',').map((item: any) => item.toLowerCase())); // 限制上傳文件的格式 小寫(xiě)后綴
const fileList: any = ref([]);
const fileList1: any = ref([]);
const fileListLength = ref(0);

const progress = ref({ "curr": 0, "color": "orange" })


watch(fileList1, (newVal, oldVal) => {
  console.log(newVal, oldVal)
  fileListLength.value = newVal.value;
  fileListLength.value = newVal.length;
}, { immediate: true, deep: true });

const httpRequest = (options: any) => {
  let nameList: Array<any> = [];
  fileList1.value.forEach((item: any) => {
      nameList.push(item.name);
  });
  const file_suffix = options.file.name.split(".");
  if (!upload_lower.value.includes(`.${file_suffix[file_suffix.length - 1]}`)) {
      public_elmsg_warning(`文件 ${options.file.name} 格式不正確`);
      return;
  }
  if (nameList.includes(options.file.name)) { }
  else {
      fileList1.value.push(options.file)
  }
  fileList.value = fileList1.value;
}

const removeFile = () => {
  fileList.value = [];
  fileList1.value = [];
  progress.value.curr = 0;
}


const create = () => {
  const formData = new FormData()
  fileList1.value.forEach((file: any) => {
      console.log(file)
      formData.append('files', file)
  })

  http.post("task/create/", formData, {
      headers: { "Content-Type": "multipart/form-data" }, onUploadProgress(progressEvent: any) {
          progress.value.curr = Math.round((progressEvent.loaded * 100) / progressEvent.total)
          if (progress.value.curr == 100) { progress.value.color = 'green' }
          else { progress.value.color = 'orange' }
      },
  }).then((res: any) => {
      if (res.code == 0) {
          public_elmsg_success("任務(wù)創(chuàng)建成功")
      }
      else { public_elmsg_error(res.msg) }
  }
  );
}
</script>

v3版本的 djagno 代碼 

from loguru import logger
from django.http.response import JsonResponse
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
    def create_task(request):
        files = request.FILES.getlist('files')
        for fit in files:
        logger.info(f"name: {fit.name} size: {round(fit.size/ 1024 / 1024 / 1024, 5)} G")
        # 保存文件
        #  with open(f"{os.sep.join(['.', fit['name']])}", mode="wb") as f:
        #         f.write(fit)
        return JsonResponse({"code": 0, "msg": "success"})

到此這篇關(guān)于vue3 + element-plus 的 upload + axios + django 文件上傳并保存的文章就介紹到這了,更多相關(guān)vue3  文件上傳并保存內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue中遇到scrollIntoView無(wú)效問(wèn)題及解決

    vue中遇到scrollIntoView無(wú)效問(wèn)題及解決

    這篇文章主要介紹了vue中遇到scrollIntoView無(wú)效問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • HBuilder導(dǎo)入vue項(xiàng)目并通過(guò)域名訪問(wèn)的過(guò)程詳解

    HBuilder導(dǎo)入vue項(xiàng)目并通過(guò)域名訪問(wèn)的過(guò)程詳解

    這篇文章主要介紹了HBuilder導(dǎo)入vue項(xiàng)目并通過(guò)域名訪問(wèn),一般情況下運(yùn)行vue項(xiàng)目需要安裝node.js,通過(guò)npm命令來(lái)安裝vue組件和運(yùn)行vue項(xiàng)目,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • vue?底部footer導(dǎo)航組件問(wèn)題

    vue?底部footer導(dǎo)航組件問(wèn)題

    這篇文章主要介紹了vue?底部footer導(dǎo)航組件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue中echarts引入中國(guó)地圖的案例

    vue中echarts引入中國(guó)地圖的案例

    這篇文章主要介紹了vue中echarts引入中國(guó)地圖的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • 使用vue編寫(xiě)h5公眾號(hào)跳轉(zhuǎn)小程序的實(shí)現(xiàn)代碼

    使用vue編寫(xiě)h5公眾號(hào)跳轉(zhuǎn)小程序的實(shí)現(xiàn)代碼

    這篇文章主要介紹了使用vue編寫(xiě)h5公眾號(hào)跳轉(zhuǎn)小程序,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • vue2里面ref的具體使用方法

    vue2里面ref的具體使用方法

    本篇文章主要介紹了vue2里面ref的具體使用方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-10-10
  • Vue導(dǎo)出json數(shù)據(jù)到Excel電子表格的示例

    Vue導(dǎo)出json數(shù)據(jù)到Excel電子表格的示例

    本篇主要介紹了Vue導(dǎo)出json數(shù)據(jù)到Excel電子表格的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • vue3.0中給自己添加一個(gè)vue.config.js配置文件

    vue3.0中給自己添加一個(gè)vue.config.js配置文件

    這篇文章主要介紹了vue3.0中給自己添加一個(gè)vue.config.js配置文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • vue項(xiàng)目開(kāi)啟Gzip壓縮和性能優(yōu)化操作

    vue項(xiàng)目開(kāi)啟Gzip壓縮和性能優(yōu)化操作

    這篇文章主要介紹了vue項(xiàng)目開(kāi)啟Gzip壓縮和性能優(yōu)化操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • 淺談Vue2.0父子組件間事件派發(fā)機(jī)制

    淺談Vue2.0父子組件間事件派發(fā)機(jī)制

    本篇文章主要介紹了淺談Vue2.0父子組件間事件派發(fā)機(jī)制,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01

最新評(píng)論

沈丘县| 清新县| 襄汾县| 谷城县| 玛多县| 衢州市| 昌邑市| 耿马| 新营市| 建德市| 通许县| 安岳县| 二手房| 托克逊县| 霍林郭勒市| 怀来县| 旅游| 卢氏县| 涟水县| 崇左市| 阳朔县| 大化| 梓潼县| 湟源县| 临安市| 台前县| 开原市| 鹤庆县| 杭锦后旗| 建阳市| 抚远县| 巴青县| 社会| 容城县| 竹北市| 南澳县| 晋中市| 监利县| 山丹县| 静宁县| 福建省|