.net 7 上传文件踩坑

news/2025/1/18 11:52:53/

(Name = “file”)
没加上这个传不进文件

		/// <summary>/// 上传单个文件/// </summary>/// <param name="formFile"></param>/// <returns></returns>[HttpPost("UploadFiles")][FunctionAttribute(MuType.Btn, "上传单个文件")]public async Task<JsonResult> UploadFiles([FromForm(Name = "file")] IFormFile formFile){var suffix = Path.GetExtension(formFile.FileName);var fileNmae = formFile.FileName;var filePaths = GetFileName(FileType.ProfilePhotoFileUpload, suffix);if (!Directory.Exists(Path.GetDirectoryName(filePaths.Item1))){Directory.CreateDirectory(Path.GetDirectoryName(filePaths.Item1));}try{using FileStream fileStream = System.IO.File.Create(filePaths.Item1);await formFile.CopyToAsync(fileStream);return await Task.FromResult(new JsonResult(new JsonResult(new ApiDataResult<string>(){Success = true,Message = "文件上传成功",Data = $"{filePaths.Item2}"  //返回的文件保存地址,用作保存数据库的})));}catch (Exception ex){return await Task.FromResult(new JsonResult(new JsonResult(new ApiDataResult<string>(){Message = "文件上传失败了"})));}}/// <summary>/// 统一文件命名  类型文件夹名/年月日/yyyyMMddHHmmss_guid.后缀/// </summary>/// <param name="fileType">存储文件夹的名字</param>/// <param name="suffix">文件后缀</param>/// <returns>第一个返回值(绝对路径),第二个返回值(相对路径)</returns>private static (string, string) GetFileName(FileType fileType, string suffix){string saveFilePath = $"{fileType.ToString()}\\{DateTime.Now.ToString("yyyyMMdd")}\\{DateTime.Now.ToString("yyyyMMddHHmmss")}_{Guid.NewGuid().ToString().ToUpper()}{suffix}";string allFilePaht = $"{Directory.GetCurrentDirectory()}\\{saveFilePath}";return (allFilePaht, saveFilePath);}

http://www.ppmy.cn/news/1184101.html

相关文章

D-Nerf:用于动态场景表示的神经辐射场

Pumarola A, Corona E, Pons-Moll G, et al. D-nerf: Neural radiance fields for dynamic scenes[C]//Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition. 2021: 10318-10327. D-Nerf 较 NeRF 的改进 1 就是能够建模移动或变形的物体&#…

『力扣刷题本』:反转链表

一、题目 给你单链表的头节点 head &#xff0c;请你反转链表&#xff0c;并返回反转后的链表。 示例 1&#xff1a; 输入&#xff1a;head [1,2,3,4,5] 输出&#xff1a;[5,4,3,2,1]示例 2&#xff1a; 输入&#xff1a;head [1,2] 输出&#xff1a;[2,1]示例 3&#xff1a;…

IOC课程整理-20 Spring 应用上下文生命周期

0.目录 1. Spring 应用上下文启动准备阶段 2. BeanFactory 创建阶段 3. BeanFactory 准备阶段 4. BeanFactory 后置处理阶段 5. BeanFactory 注册 BeanPostProcessor 阶段 6. 初始化內建 Bean&#xff1a;MessageSource 7. 初始化內建 Bean&#xff1a;Spring 事件广播器…

【教3妹学编程-java实战5】结构体字段赋值的几种方式

插&#xff1a; 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到网站。 坚持不懈&#xff0c;越努力越幸运&#xff0c;大家一起学习鸭~~~ 2哥 :3妹&#xff0c;考考你&#xff0c;你知道java结…

基础课13——数据异常处理

数据异常是指数据不符合预期或不符合常识的情况。数据异常可能会导致数据分析结果不准确&#xff0c;甚至是错误&#xff0c;因此在进行数据分析之前需要对数据进行清洗和验证。 常见的数据异常包括缺失值、重复值、异常值等。 缺失值是指数据中存在未知值或未定义的值&#…

electron汇总

python3自带了pip pip search已经被禁用&#xff0c;安装pip—— pip install pip-searchpython3.x移除了distutils 管理员权限下运行cmd&#xff0c;运行以下命令 // 修改pip镜像地址 pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ // 安装 Set…

在vscode中运行c++代码,windows 11系统

缘起 工作中用mac电脑&#xff0c;装了vscode和c&#xff0c;在家里的windows电脑上&#xff0c;也想运行vscode&#xff0c;这时候就遇到问题了。现在将问题解决方案记录下来&#xff0c;以供参考。 解决方案 开始比较懒&#xff0c;直接百度一下&#xff0c;找了几篇文章&…

0146 网络层

目录 4 网络层 4.1 网络层的功能 4.2 路由算法与路由协议 4.3 IPv4 4.4 IPv6 4 网络层 4.1 网络层的功能 4.2 路由算法与路由协议 4.3 IPv4 4.4 IPv6 部分习题 1.网络层的主要目的是&#xff08;&#xff09; A.在邻接结点间进行数据报传输 B.在邻接结点间进行数…