😎优化base64文件上传
This commit is contained in:
parent
d686e3c32d
commit
ee9f0a1798
@ -76,18 +76,22 @@ public class SysFileService : IDynamicApiController, ITransient
|
|||||||
[DisplayName("上传文件Base64")]
|
[DisplayName("上传文件Base64")]
|
||||||
public async Task<SysFile> UploadFileFromBase64(UploadFileFromBase64Input input)
|
public async Task<SysFile> UploadFileFromBase64(UploadFileFromBase64Input input)
|
||||||
{
|
{
|
||||||
byte[] fileData = Convert.FromBase64String(input.FileDataBase64);
|
var pattern = @"data:(?<type>.+?);base64,(?<data>[^""]+)";
|
||||||
|
var regex = new Regex(pattern, RegexOptions.Compiled);
|
||||||
|
var match = regex.Match(input.FileDataBase64);
|
||||||
|
|
||||||
|
byte[] fileData = Convert.FromBase64String(match.Groups["data"].Value);
|
||||||
|
var contentType = match.Groups["type"].Value;
|
||||||
|
if (string.IsNullOrEmpty(input.FileName))
|
||||||
|
input.FileName = $"{YitIdHelper.NextId()}.{contentType.AsSpan(contentType.LastIndexOf('/') + 1)}";
|
||||||
|
|
||||||
var ms = new MemoryStream();
|
var ms = new MemoryStream();
|
||||||
ms.Write(fileData);
|
ms.Write(fileData);
|
||||||
ms.Seek(0, SeekOrigin.Begin);
|
ms.Seek(0, SeekOrigin.Begin);
|
||||||
if (string.IsNullOrEmpty(input.FileName))
|
|
||||||
input.FileName = $"{YitIdHelper.NextId()}.jpg";
|
|
||||||
if (string.IsNullOrEmpty(input.ContentType))
|
|
||||||
input.ContentType = "image/jpg";
|
|
||||||
IFormFile formFile = new FormFile(ms, 0, fileData.Length, "file", input.FileName)
|
IFormFile formFile = new FormFile(ms, 0, fileData.Length, "file", input.FileName)
|
||||||
{
|
{
|
||||||
Headers = new HeaderDictionary(),
|
Headers = new HeaderDictionary(),
|
||||||
ContentType = input.ContentType
|
ContentType = contentType
|
||||||
};
|
};
|
||||||
return await UploadFile(new FileUploadInput { File = formFile, Path = input.Path, FileType = input.FileType, IsPublic = input.IsPublic });
|
return await UploadFile(new FileUploadInput { File = formFile, Path = input.Path, FileType = input.FileType, IsPublic = input.IsPublic });
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user