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

后端“fastapi”+前端“vue3+ts+ElementPlus”上傳文件到uploads目錄

這篇具有很好參考價值的文章主要介紹了后端“fastapi”+前端“vue3+ts+ElementPlus”上傳文件到uploads目錄。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

一、后端fastapi

確保已安裝好python3和fastapi

python -m pip install 'fastapi[all]'

mail.py

from fastapi import FastAPI, File, UploadFile
from fastapi.responses import FileResponse
import os

app = FastAPI()

@app.post("/upload")
async def create_upload_file(file: UploadFile = File(...)):
    dirs = 'uploads'
    # 判斷uploads目錄是否存在,否則新建uploads目錄
    if not os.path.exists(dirs):
        os.makedirs(dirs)
    # 保存上傳文件到uploads目錄
    file_location = f"{dirs}/{file.filename}"
    with open(file_location, "wb") as file_object:
        file_object.write(file.file.read())
    return {"filename": file.filename}

運行fastapi服務(wù)器

python -m uvicorn main:app --reload

使用瀏覽器訪問?http://127.0.0.1:8000/http://127.0.0.1:8000/docs

fastapi +vue,fastapi,vue

二、前端vue3

確保已安裝node.js和yarn

npm install -g yarn

使用vite初始化前端目錄

PS C:\Users\airdot\source\repos\testweb> yarn create vite
yarn create v1.22.21
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Installed "create-vite@5.0.0" with binaries:
      - create-vite
      - cva
√ Project name: ... web
√ Select a framework: ? Vue
√ Select a variant: ? TypeScript

Scaffolding project in C:\Users\airdot\source\repos\testweb\web...

Done. Now run:

  cd web
  yarn
  yarn dev

Done in 5.55s.

PS C:\Users\airdot\source\repos\testweb>cd web
PS C:\Users\airdot\source\repos\testweb\web>yarn

?安裝element-plus

yarn add element-plus

main.ts中導(dǎo)入element-plus

import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'

const app = createApp(App)

app.use(ElementPlus)
app.mount('#app')

?修改vite.config.ts配置“CORS 跨域”

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  server:{
    host:"127.0.0.1",
    port:7001,
    open:true,    
    proxy:{
      '/api':{
        target:"http://localhost:8000/",
        changeOrigin:true,
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  }
})

修改App.vue


<template>
  <el-upload 
    ref="upload" 
    class="upload-demo" 
    action="api/upload" 
    :limit="1" 
    :on-exceed="handleExceed"
    :auto-upload="false">
    <template #trigger>
      <el-button type="primary">select file</el-button>
    </template>
    <el-button class="ml-3" type="success" @click="submitUpload">
      upload to server
    </el-button>
    <template #tip>
      <div class="el-upload__tip text-red">
        limit 1 file, new file will cover the old file
      </div>
    </template>
  </el-upload>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { genFileId } from 'element-plus'
import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus'

const upload = ref<UploadInstance>()

const handleExceed: UploadProps['onExceed'] = (files) => {
  upload.value!.clearFiles()
  const file = files[0] as UploadRawFile
  file.uid = genFileId()
  upload.value!.handleStart(file)
}

const submitUpload = () => {
  upload.value!.submit()
}
</script>

運行

yarn dev

使用瀏覽器訪問?http://127.0.0.1:7001/

fastapi +vue,fastapi,vue文章來源地址http://www.zghlxwxcb.cn/news/detail-834643.html

到了這里,關(guān)于后端“fastapi”+前端“vue3+ts+ElementPlus”上傳文件到uploads目錄的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

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

相關(guān)文章

  • vue3+ts - element-plus封裝上傳文件圖片組件

    vue3+ts - element-plus封裝上傳文件圖片組件

    ??近期做到的項目中有涉及到上傳圖片上傳文件的需求,因為是pc管理后臺,用到了element-plus框架,所以我也一起使用element-plus中的上傳圖片上傳圖片功能,并對它進行封裝成一個組件,方便在多個地方使用。 1、上傳文件、視頻 2、上傳圖片 ??在這里上傳圖片和文件是分

    2024年02月12日
    瀏覽(36)
  • 【Java實現(xiàn)文件上傳】java后端+vue前端實現(xiàn)文件上傳全過程詳解(附源碼)

    【Java實現(xiàn)文件上傳】java后端+vue前端實現(xiàn)文件上傳全過程詳解(附源碼)

    【 寫在前面 】其實這篇文章我早就想寫了,只是一直被需求開發(fā)耽擱,這不晚上剛好下班后有點時間,記錄一下。需求是excel表格的上傳,這個是很多業(yè)務(wù)系統(tǒng)不可或缺的功能點,再此也希望您能夠讀完我這篇文章對文件上傳不再困惑。(文件下載見另外一篇) 涉及知識點

    2024年02月06日
    瀏覽(30)
  • Vue3 Vite4 ElementPlus TS模板(含Vue-Router4+Pinia4)

    Vue3 Vite4 ElementPlus TS模板(含Vue-Router4+Pinia4)

    手動安裝配置Vue3 ElementPlus模板比較繁瑣,網(wǎng)上尋找一些模板不太符合自己預(yù)期,因此花點精力搭建一個符合自己需求的架子 采用最新的組件,版本如下: vite 4.3.9 vite-plugin-mock 2.9.8 vue 3.3.4 pinia 2.1.3 vue-router 4.2.2 element-plus 2.3.6 滿足自己以下功能: Vite工具熱啟動速度快,修改后

    2024年02月08日
    瀏覽(24)
  • 【新項目開發(fā)】vue3+ts+elementPlus+ffmpegjs開發(fā)純web端的視頻編輯器

    當(dāng)在項目中使用新技術(shù)時,我們應(yīng)該首先進行調(diào)研,了解其特點和使用方法。在實現(xiàn)功能時,我們可以采用最簡單的方式,而不必過于關(guān)注項目的設(shè)計和結(jié)構(gòu)。一旦掌握了新技術(shù),我們可以根據(jù)其API屬性進行代碼設(shè)計,以便更好地開發(fā)。以開發(fā)一個純web端的視頻編輯處理器為

    2024年02月15日
    瀏覽(85)
  • vue3+elementPlus登錄向后端服務(wù)器發(fā)起數(shù)據(jù)請求Ajax

    vue3+elementPlus登錄向后端服務(wù)器發(fā)起數(shù)據(jù)請求Ajax

    后端的url登錄接口 先修改main.js文件 login.vue 此時前端有跨域問題 先配置跨域 vue.config.js? 項目中如果沒有這個文件 請自行創(chuàng)建。 此時可以看到跳轉(zhuǎn)到登錄到home頁面 ?完整的后端登錄方法? 消息提示使用elementPlus 的Elmessage?

    2024年01月18日
    瀏覽(28)
  • 前端vue elementUI upload上傳組件封裝&多文件上傳&進度條,后端servlet request.getPart()接收文件信息

    前端vue elementUI upload上傳組件封裝&多文件上傳&進度條,后端servlet request.getPart()接收文件信息

    選中多個文件上傳 通過 axios請求 onUploadProgress 方法監(jiān)聽 on-progress on-success 用這兩個鉤子函數(shù)實現(xiàn)進度條 下面有對應(yīng)的函數(shù)。 本文是每個文件一個請求上傳 也可以用一個請求上傳多個文件,需要將文件遍歷添加到 form 表單中,后端用 request.getParts(); 獲取集合,有需要的可以改

    2024年02月11日
    瀏覽(33)
  • Vue3使用ElementPlus中的el-upload手動上傳并調(diào)用上傳接口

    實體類 定義接口 上傳文件并插入數(shù)據(jù)庫數(shù)據(jù)

    2024年01月20日
    瀏覽(98)
  • vue3+elementplus前端生成圖片驗證碼

    vue3+elementplus前端生成圖片驗證碼

    1、安裝 使用npm i identify --save 或者 yarn add identify --save 2、新建vue組件components/identify/identify.vue 3、一般是登錄頁面用到這個,在你的登錄頁面的from表單的相應(yīng)位置加上填寫驗證碼的html 4、在script下引入組件,并編寫方法 5、效果 ?

    2024年01月20日
    瀏覽(27)
  • vue3 + TS + elementplus + pinia實現(xiàn)后臺管理系統(tǒng)左側(cè)菜單聯(lián)動實現(xiàn) tab根據(jù)路由切換聯(lián)動內(nèi)容

    vue3 + TS + elementplus + pinia實現(xiàn)后臺管理系統(tǒng)左側(cè)菜單聯(lián)動實現(xiàn) tab根據(jù)路由切換聯(lián)動內(nèi)容

    效果圖: ?home.vue頁面代碼 left.vue頁面代碼 tab.vue頁面代碼 pinia里面的代碼 安裝 使用插件 ?在main.ts中注冊 路由代碼 我把代碼放git上了,有需要的自行拉取 https://gitee.com/Flechazo7/vue3.git

    2024年02月09日
    瀏覽(27)
  • 純前端使用Vue3上傳文件到minio文件服務(wù)器,粘貼直接可用

    1、首先安裝minio的插件,因為我使用的vue3,不支持模塊化的導(dǎo)入,所以我們使用一個別人寫好的vue2的包 2、在需要上傳文件的頁面導(dǎo)入這個包 3、創(chuàng)建一個minio的客戶端 這里說明一下,accessKey、secretKey、sessionToken都是通過接口獲取到的 臨時憑證 4、 通過帶預(yù)簽名的url上傳,首

    2024年04月11日
    瀏覽(25)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

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

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包