You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
2.0 KiB
C#
68 lines
2.0 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace Ds.Module.File.Model
|
|
{
|
|
public class DsUserFile
|
|
{
|
|
/// <summary>
|
|
/// 文件名
|
|
/// </summary>
|
|
public string FileName { get; set; } = "Ds_001";
|
|
|
|
/// <summary>
|
|
/// 文件大小
|
|
/// </summary>
|
|
public long Length { get; set; }
|
|
|
|
/// <summary>
|
|
/// 文件扩展名
|
|
/// </summary>
|
|
public string Extension { get; set; } = "_xxx_";
|
|
|
|
/// <summary>
|
|
/// 文件类型
|
|
/// </summary>
|
|
public string FileType { get; set; } = ".png";
|
|
|
|
private static readonly string[] Filters = { ".jpg", ".png", ".bmp" };
|
|
public bool IsValid => !string.IsNullOrEmpty(this.Extension) && Filters.Contains(this.Extension);
|
|
|
|
private IFormFile file;
|
|
|
|
public IFormFile File
|
|
{
|
|
get { return file; }
|
|
set
|
|
{
|
|
if (value != null)
|
|
{
|
|
this.file = value;
|
|
|
|
this.FileType = this.file.ContentType;
|
|
this.Length = this.file.Length;
|
|
this.Extension = this.file.FileName.Substring(file.FileName.LastIndexOf('.'));
|
|
if (string.IsNullOrEmpty(this.FileName))
|
|
this.FileName = this.FileName;
|
|
}
|
|
}
|
|
}
|
|
|
|
public async Task<string> SaveAs(string destinationDir = null)
|
|
{
|
|
if (this.file == null)
|
|
throw new ArgumentNullException("没有需要保存的文件");
|
|
|
|
if (destinationDir != null)
|
|
Directory.CreateDirectory(destinationDir);
|
|
|
|
var newName = DateTime.Now.Ticks;
|
|
var newFile = Path.Combine(destinationDir ?? "", $"{newName}{this.Extension}");
|
|
using (FileStream fs = new FileStream(newFile, FileMode.CreateNew))
|
|
{
|
|
await this.file.CopyToAsync(fs);
|
|
fs.Flush();
|
|
}
|
|
return newFile;
|
|
}
|
|
}
|
|
} |