tuiwucarrer/Admin.NET/Admin.NET.Core/Util/CustomFormFile.cs

50 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
namespace Admin.NET.Core;
public class CustomFormFile : IFormFile
{
private readonly Stream _stream;
public CustomFormFile(byte[] fileContents, string fileName, string contentType, string name)
{
_stream = new MemoryStream(fileContents);
FileName = fileName;
ContentType = contentType;
Length = fileContents.Length;
Name = name;
Headers = new HeaderDictionary(); // 初始化 Headers
ContentDisposition = $"form-data; name=\"{name}\"; filename=\"{fileName}\""; // 初始化 ContentDisposition
}
public string ContentDisposition { get; }
public string ContentType { get; }
public string FileName { get; }
public bool Exists => true;
public long Length { get; }
public string Name { get; }
public IHeaderDictionary Headers { get; }
public Stream OpenReadStream() => _stream;
public Task CopyToAsync(Stream target, CancellationToken cancellationToken = default)
{
_stream.Seek(0, SeekOrigin.Begin); // Reset the stream position
return _stream.CopyToAsync(target, 81920, cancellationToken);
}
public void CopyTo(Stream target)
{
_stream.Seek(0, SeekOrigin.Begin); // Reset the stream position
_stream.CopyTo(target);
}
}