0


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

一、后端fastapi

确保已安装好python3和fastapi

  1. python -m pip install 'fastapi[all]'

mail.py

  1. from fastapi import FastAPI, File, UploadFile
  2. from fastapi.responses import FileResponse
  3. import os
  4. app = FastAPI()
  5. @app.post("/upload")
  6. async def create_upload_file(file: UploadFile = File(...)):
  7. dirs = 'uploads'
  8. # 判断uploads目录是否存在,否则新建uploads目录
  9. if not os.path.exists(dirs):
  10. os.makedirs(dirs)
  11. # 保存上传文件到uploads目录
  12. file_location = f"{dirs}/{file.filename}"
  13. with open(file_location, "wb") as file_object:
  14. file_object.write(file.file.read())
  15. return {"filename": file.filename}

运行fastapi服务器

  1. python -m uvicorn main:app --reload

使用浏览器访问 http://127.0.0.1:8000/http://127.0.0.1:8000/docs

二、前端vue3

确保已安装node.js和yarn

  1. npm install -g yarn

使用vite初始化前端目录

  1. PS C:\Users\airdot\source\repos\testweb> yarn create vite
  2. yarn create v1.22.21
  3. [1/4] Resolving packages...
  4. [2/4] Fetching packages...
  5. [3/4] Linking dependencies...
  6. [4/4] Building fresh packages...
  7. success Installed "create-vite@5.0.0" with binaries:
  8. - create-vite
  9. - cva
  10. Project name: ... web
  11. Select a framework: » Vue
  12. Select a variant: » TypeScript
  13. Scaffolding project in C:\Users\airdot\source\repos\testweb\web...
  14. Done. Now run:
  15. cd web
  16. yarn
  17. yarn dev
  18. Done in 5.55s.
  19. PS C:\Users\airdot\source\repos\testweb>cd web
  20. PS C:\Users\airdot\source\repos\testweb\web>yarn

安装element-plus

  1. yarn add element-plus

main.ts中导入element-plus

  1. import { createApp } from 'vue'
  2. import ElementPlus from 'element-plus'
  3. import 'element-plus/dist/index.css'
  4. import App from './App.vue'
  5. const app = createApp(App)
  6. app.use(ElementPlus)
  7. app.mount('#app')

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

  1. import { defineConfig } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. // https://vitejs.dev/config/
  4. export default defineConfig({
  5. plugins: [vue()],
  6. server:{
  7. host:"127.0.0.1",
  8. port:7001,
  9. open:true,
  10. proxy:{
  11. '/api':{
  12. target:"http://localhost:8000/",
  13. changeOrigin:true,
  14. rewrite: (path) => path.replace(/^\/api/, '')
  15. }
  16. }
  17. }
  18. })

修改App.vue

  1. <template>
  2. <el-upload
  3. ref="upload"
  4. class="upload-demo"
  5. action="api/upload"
  6. :limit="1"
  7. :on-exceed="handleExceed"
  8. :auto-upload="false">
  9. <template #trigger>
  10. <el-button type="primary">select file</el-button>
  11. </template>
  12. <el-button class="ml-3" type="success" @click="submitUpload">
  13. upload to server
  14. </el-button>
  15. <template #tip>
  16. <div class="el-upload__tip text-red">
  17. limit 1 file, new file will cover the old file
  18. </div>
  19. </template>
  20. </el-upload>
  21. </template>
  22. <script lang="ts" setup>
  23. import { ref } from 'vue'
  24. import { genFileId } from 'element-plus'
  25. import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus'
  26. const upload = ref<UploadInstance>()
  27. const handleExceed: UploadProps['onExceed'] = (files) => {
  28. upload.value!.clearFiles()
  29. const file = files[0] as UploadRawFile
  30. file.uid = genFileId()
  31. upload.value!.handleStart(file)
  32. }
  33. const submitUpload = () => {
  34. upload.value!.submit()
  35. }
  36. </script>

运行

  1. yarn dev

使用浏览器访问 http://127.0.0.1:7001/

标签: fastapi vue

本文转载自: https://blog.csdn.net/airdot/article/details/134623780
版权归原作者 airdot 所有, 如有侵权,请联系我们删除。

“后端“fastapi”+前端“vue3+ts+ElementPlus”上传文件到uploads目录”的评论:

还没有评论