.net 7 上传文件踩坑

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

		/// 
        /// 上传单个文件
        /// 
        /// 
        /// 
        [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 = "文件上传失败了"
                 })
                 ));
            }
        }

        /// 
        /// 统一文件命名  类型文件夹名/年月日/yyyyMMddHHmmss_guid.后缀
        /// 
        /// 存储文件夹的名字
        /// 文件后缀
        /// 第一个返回值(绝对路径),第二个返回值(相对路径)
        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);
        }

你可能感兴趣的:(.net,c#)