前端文件上传全过程

embedded/2024/10/20 10:35:28/

特别说明:ui框架使用的是蚂蚁的antd 这里主要是学习前端上传接口的传递参数包括前端上传之前对于代码的整理

一、第一步将前端页面画出来
在这里插入图片描述
源代码:

/** 费用管理 - IT费用管理 - 费用数据上传 */
import { useState } from "react";
import { WARNING_INFO } from "@/constants";
import { InboxOutlined } from "@ant-design/icons";
import { Button, Card, DatePicker, message, Spin, Upload, UploadProps } from "antd";
import dayjs from 'dayjs'
import './index.less'
import { ITDataUpload } from "@/services/costControl";const DataUpload = () => {const [loading, setLoading] = useState<boolean>(false);const [fileList, setFileList] = useState<any>([]); //上传的文件列表const [dateString, setDateString] = useState<string>(''); // 数据日期// 文件组件属性const uploadProps: UploadProps = {multiple: false,maxCount: 1,onChange(info) {},beforeUpload: (file) => {console.log('上传文件', file);const regExp = /^.*\.(?:xls|xlsx)$/iconst isExcel = regExp.test(file.name)if (!isExcel) {message.error(WARNING_INFO.EXCEL_INFO);return Upload.LIST_IGNORE}// if (file.size > 1024 * 1024 * 10) {//     message.error(`${file.name}大小不能超过10M`);//     return Upload.LIST_IGNORE;// }// 通过校验开始上传setFileList([file])// 阻止自动上传return false;},onRemove: () => {//console.log('删除');setFileList([])},onDrop(e) {//console.log('Dropped files', e.dataTransfer.files);},};const handleUpload = async (fileList: any) => {if (!dateString) {message.error('请选择数据日期')} else {//console.log('时间',dateString);//console.log('文件',fileList);try {setLoading(true);let res = await ITDataUpload({ date: dateString, file: fileList })//console.log('上传文件res', res);if (res?.code == 200) {message.success('数据文件上传成功')setLoading(false);setFileList([])setDateString('')} else {message.error('数据文件上传失败')setLoading(false);}} catch {setLoading(false);}}}const onChange = (date: any, dateString: any) => {setDateString(dayjs(dateString, 'YYYY-MM').format("YYYY-MM-01"))}return (<Spin tip="loading..." spinning={loading}><Card bordered={false} style={{ width: '100%', minHeight: 'calc(100vh - 196px)', paddingTop: '60px', paddingLeft: '80px' }}><div className="dateControl"><span className="title">数据日期:</span><DatePickeronChange={onChange}style={{ width: 200 }}picker="month"value={dateString ? dayjs(dateString) : ''} /></div><div className='uploadAndDownload'><span>上传文件:</span><Upload.Dragger {...uploadProps}fileList={fileList}className="draggerStyle"><p className="ant-upload-drag-icon"><InboxOutlined /></p><p className="ant-upload-text">点击或者将文件拖拽到这里上传</p><p style={{ color: '#0000006e' }}>支持扩展名:xlsx,xls</p></Upload.Dragger></div><div className='buttonOption'><Button type="primary" onClick={() => {//console.log('点击提交', fileList);if (fileList.length > 0) {handleUpload(fileList[0])} else {message.error('请上传文件!')}}}>提交</Button><Button onClick={() => {//console.log('点击重置');setFileList([])setDateString('')}}>重置</Button></div></Card></Spin>)
}export default DataUpload

二、第二步将封装上传接口的函数整理出来(这个是核心)

在这里插入图片描述
源码:

 const handleUpload = async (fileList: any) => {if (!dateString) {message.error('请选择数据日期')} else {//console.log('时间',dateString);//console.log('文件',fileList);try {setLoading(true);let res = await ITDataUpload({ date: dateString, file: fileList })//console.log('上传文件res', res);if (res?.code == 200) {message.success('数据文件上传成功')setLoading(false);setFileList([])setDateString('')} else {message.error('数据文件上传失败')setLoading(false);}} catch {setLoading(false);}}}

三、第三步将封装的接口弄明白
在这里插入图片描述
源代码:

export const ITDataUpload = async (data: any) => {return request(costControlApi.ITDataUpload, {method: 'POST',data,headers: { 'Content-Type': 'multipart/form-data' },requestType: 'form',})
}

四、第四步将上传接口弄明白
在这里插入图片描述
五、第五步将上传结果演示
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

🆗呀,这个就是整个的前端部分的文件上传,需要注意的是对上传接口的处理,其它的倒没有什么,相对于文件下载需要对接收到的数据流还要处理而言,整个文件上传还是异常简单的有没有感觉到。


http://www.ppmy.cn/embedded/118121.html

相关文章

pytorch学习笔记二:用pytorch神经网络模型做气温预测、分类任务构建和分类网络构建、卷积神经网络原理介绍

文章目录 一、搭建pytorch神经网络进行气温预测1&#xff09;基础搭建2&#xff09;实际操作标识特征和标签3&#xff09;构建成标准化的预处理数据&#xff08;做标准化收敛速度更快&#xff09; 二、按照建模顺序构建完成网络架构1&#xff09;np.array格式的标签(y)和特征(x…

C语言线程编程深度解析

文章目录 前言一、线程基础概念1. 什么是线程&#xff1f;2. 线程与进程的区别 二、POSIX线程库&#xff08;pthread&#xff09;1. pthread简介2. 编译与链接3. 创建线程示例代码&#xff1a; 4. 线程同步互斥锁&#xff08;Mutex&#xff09;示例代码&#xff1a; 条件变量&a…

【Roblox/Lua】Roblox抽奖游戏设计概述

文章目录 Roll功能RollPanelUIRollAnimRollManagerPlayerBackpackBagUI 物品检视功能&#xff08;检视&#xff0c;锁定&#xff0c;装备&#xff0c;删除&#xff09;ItemUIInfoUI 自动抽奖快速抽奖快速移动 部分实现细节新增删除背包面板玩家初始化 代码总览数据存储部分服务…

nlp大语言模型原理

NLP&#xff08;自然语言处理&#xff09;的主要任务可以分为以下几个方面‌&#xff1a; ‌词法分析&#xff08;Lexical Analysis&#xff09;‌&#xff1a;这是NLP的基础&#xff0c;包括分词&#xff08;Tokenization&#xff09;、词性标注&#xff08;Part-of-Speech Ta…

利用低代码快速搭建电商小程序之商品列表页

目标&#xff1a; 搭建商城的一个商品列表页面&#xff08;先做静态页&#xff09; 开发环境&#xff1a; 访问白码低代码平台&#xff1a;https://www.bnocode.com/ 白码的新自定义页功能&#xff08;使用vue框架&#xff09; 前期准备&#xff1a; 需要先准备商品数据表…

数据结构 ——— 常见的时间复杂度计算例题(中篇)

目录 例题1&#xff1a; 例题2&#xff1a; 例题1&#xff1a; 代码演示&#xff1a; void BubbleSort(int* a, int n) {// 断言assert(a);// 循环1for (size_t end n; end > 0; end--){int flag 0;// 循环2&#xff08;循环1的内部循环&#xff09;for (size_t i 1;…

FFmpeg开发笔记(五十八)把32位采样的MP3转换为16位的PCM音频

《FFmpeg开发实战&#xff1a;从零基础到短视频上线》一书的“5.1.2 把音频流保存为PCM文件”介绍了如何把媒体文件中的音频流转存为原始的PCM音频&#xff0c;在样例代码的转存过程中&#xff0c;解码后的PCM数据未经任何加工处理&#xff0c;就直接保存到二进制文件。也就是…

伊犁-linux 硬盘添加,分区,格式化

主要是linux 下操作硬盘分区&#xff0c;格式化 这样1个sata 盘就添加成功了 &#xff01;  继续添加三块 sata1 hda sata hdb sata hdc sata hdd scsi sda 作为启动盘 进行操作系统的引导 如果scsi 往下调整 先敲enter 在用&#xff0d; 号往下 如果是往上调整敲…