后端“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服务器

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中导入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文章来源地址https://www.toymoban.com/news/detail-834643.html

到了这里,关于后端“fastapi”+前端“vue3+ts+ElementPlus”上传文件到uploads目录的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • vue3+ts - element-plus封装上传文件图片组件

      近期做到的项目中有涉及到上传图片上传文件的需求,因为是pc管理后台,用到了element-plus框架,所以我也一起使用element-plus中的上传图片上传图片功能,并对它进行封装成一个组件,方便在多个地方使用。 1、上传文件、视频 2、上传图片   在这里上传图片和文件是分

    2024年02月12日
    浏览(22)
  • 【Java实现文件上传】java后端+vue前端实现文件上传全过程详解(附源码)

    【 写在前面 】其实这篇文章我早就想写了,只是一直被需求开发耽搁,这不晚上刚好下班后有点时间,记录一下。需求是excel表格的上传,这个是很多业务系统不可或缺的功能点,再此也希望您能够读完我这篇文章对文件上传不再困惑。(文件下载见另外一篇) 涉及知识点

    2024年02月06日
    浏览(33)
  • Vue3 Vite4 ElementPlus TS模板(含Vue-Router4+Pinia4)

    手动安装配置Vue3 ElementPlus模板比较繁琐,网上寻找一些模板不太符合自己预期,因此花点精力搭建一个符合自己需求的架子 采用最新的组件,版本如下: 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日
    浏览(29)
  • 【新项目开发】vue3+ts+elementPlus+ffmpegjs开发纯web端的视频编辑器

    当在项目中使用新技术时,我们应该首先进行调研,了解其特点和使用方法。在实现功能时,我们可以采用最简单的方式,而不必过于关注项目的设计和结构。一旦掌握了新技术,我们可以根据其API属性进行代码设计,以便更好地开发。以开发一个纯web端的视频编辑处理器为

    2024年02月15日
    浏览(19)
  • vue3+elementPlus登录向后端服务器发起数据请求Ajax

    后端的url登录接口 先修改main.js文件 login.vue 此时前端有跨域问题 先配置跨域 vue.config.js  项目中如果没有这个文件 请自行创建。 此时可以看到跳转到登录到home页面  完整的后端登录方法  消息提示使用elementPlus 的Elmessage 

    2024年01月18日
    浏览(24)
  • 前端vue elementUI upload上传组件封装&多文件上传&进度条,后端servlet request.getPart()接收文件信息

    选中多个文件上传 通过 axios请求 onUploadProgress 方法监听 on-progress on-success 用这两个钩子函数实现进度条 下面有对应的函数。 本文是每个文件一个请求上传 也可以用一个请求上传多个文件,需要将文件遍历添加到 form 表单中,后端用 request.getParts(); 获取集合,有需要的可以改

    2024年02月11日
    浏览(24)
  • Vue3使用ElementPlus中的el-upload手动上传并调用上传接口

    实体类 定义接口 上传文件并插入数据库数据

    2024年01月20日
    浏览(26)
  • vue3+elementplus前端生成图片验证码

    1、安装 使用npm i identify --save 或者 yarn add identify --save 2、新建vue组件components/identify/identify.vue 3、一般是登录页面用到这个,在你的登录页面的from表单的相应位置加上填写验证码的html 4、在script下引入组件,并编写方法 5、效果  

    2024年01月20日
    浏览(23)
  • vue3 + TS + elementplus + pinia实现后台管理系统左侧菜单联动实现 tab根据路由切换联动内容

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

    2024年02月09日
    浏览(21)
  • 纯前端使用Vue3上传文件到minio文件服务器,粘贴直接可用

    1、首先安装minio的插件,因为我使用的vue3,不支持模块化的导入,所以我们使用一个别人写好的vue2的包 2、在需要上传文件的页面导入这个包 3、创建一个minio的客户端 这里说明一下,accessKey、secretKey、sessionToken都是通过接口获取到的 临时凭证 4、 通过带预签名的url上传,首

    2024年04月11日
    浏览(25)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包