feat: change file upload api

This commit is contained in:
anjoy8 2023-05-06 10:58:18 +08:00
parent 48a4adcd37
commit ddb326763b
2 changed files with 4 additions and 9 deletions

View File

@ -54,11 +54,11 @@ namespace Blog.Core.Controllers
public async Task<MessageModel<string>> InsertPicture([FromForm]UploadFileDto dto) public async Task<MessageModel<string>> InsertPicture([FromForm]UploadFileDto dto)
{ {
if (dto.Files == null || !dto.Files.Any()) return Failed("请选择上传的文件。"); if (dto.file == null || !dto.file.Any()) return Failed("请选择上传的文件。");
//格式限制 //格式限制
var allowType = new string[] { "image/jpg", "image/png", "image/jpeg" }; var allowType = new string[] { "image/jpg", "image/png", "image/jpeg" };
var allowedFile = dto.Files.Where(c => allowType.Contains(c.ContentType)); var allowedFile = dto.file.Where(c => allowType.Contains(c.ContentType));
if (!allowedFile.Any()) return Failed("图片格式错误"); if (!allowedFile.Any()) return Failed("图片格式错误");
if (allowedFile.Sum(c => c.Length) > 1024 * 1024 * 4) return Failed("图片过大"); if (allowedFile.Sum(c => c.Length) > 1024 * 1024 * 4) return Failed("图片过大");
@ -79,7 +79,7 @@ namespace Blog.Core.Controllers
} }
} }
var excludeFiles = dto.Files.Except(allowedFile); var excludeFiles = dto.file.Except(allowedFile);
if (excludeFiles.Any()) if (excludeFiles.Any())
{ {

View File

@ -1,10 +1,5 @@
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Blog.Core.Model.ViewModels namespace Blog.Core.Model.ViewModels
{ {
@ -12,7 +7,7 @@ namespace Blog.Core.Model.ViewModels
{ {
//多文件 //多文件
[Required] [Required]
public IFormFileCollection Files { get; set; } public IFormFileCollection file { get; set; }
//单文件 //单文件
//public IFormFile File { get; set; } //public IFormFile File { get; set; }