forked from NewLifeX/X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormFile.cs
46 lines (35 loc) · 1.28 KB
/
FormFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using NewLife.Data;
namespace NewLife.Http;
/// <summary>表单部分</summary>
public class FormFile
{
#region 属性
/// <summary>名称</summary>
public String Name { get; set; } = null!;
/// <summary>内容部署</summary>
public String? ContentDisposition { get; set; }
/// <summary>内容类型</summary>
public String? ContentType { get; set; }
/// <summary>文件名</summary>
public String? FileName { get; set; }
/// <summary>数据</summary>
public Packet? Data { get; set; }
/// <summary>长度</summary>
public Int64 Length => Data?.Total ?? 0;
#endregion
/// <summary>打开数据读取流</summary>
/// <returns></returns>
public Stream? OpenReadStream() => Data?.GetStream();
/// <summary>保存到文件</summary>
/// <param name="fileName"></param>
public void SaveToFile(String? fileName = null)
{
if (fileName.IsNullOrEmpty()) fileName = FileName;
if (fileName.IsNullOrEmpty()) throw new ArgumentNullException(nameof(fileName));
if (Data == null) throw new ArgumentNullException(nameof(Data));
fileName.EnsureDirectory(true);
using var fs = File.OpenWrite(fileName.GetFullPath());
Data.CopyTo(fs);
fs.SetLength(fs.Position);
}
}