[fix]修正UdpServer在接收广播时连续启动接收的错误,在StarAgent中,此时可能收到广播包,SocketFlags是Broadcast,需要清空,否则报错“参考的对象类型不支持尝试的操作”; 无需设置SocketOptionName.PacketInformation,在ReceiveMessageFromAsync时会自动设置,并且支持ipv6;
石头 编写于 2024-10-10 00:36:00 石头 提交于 2024-10-10 00:45:43
X
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);
    }
}