You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.9 KiB

using System;
using System.Collections.Generic;
using System.Text;
namespace Apewer.Web
{
/// <summary>表示 API 行为结果,主体为字节数组。</summary>
public class BytesResult : HeadResult
{
#region content
/// <summary>主体。</summary>
public virtual byte[] Bytes { get; set; }
/// <summary>创建结果实例。</summary>
public BytesResult(byte[] bytes, string contentType = "application/octet-stream") : this(200, bytes, contentType) { }
/// <summary>创建结果实例。</summary>
public BytesResult(int status, byte[] bytes, string contentType = "application/octet-stream") : base(status)
{
Headers.Add("Content-Type", contentType);
Bytes = bytes;
}
#endregion
#region execute
/// <summary>写入主体。</summary>
/// <param name="context">API 上下文。</param>
/// <param name="bodyData">主体的内容,字节数应该和 Content-Length 一致。</param>
protected virtual void WriteBody(ApiContext context, byte[] bodyData)
{
if (bodyData != null && bodyData.Length > 0)
{
var stream = context.Provider.ResponseBody();
stream.Write(bodyData, 0, bodyData.Length);
}
}
/// <summary>写入 HTTP 头和主体。</summary>
public override void ExecuteResult(ApiContext context)
{
if (Bytes == null)
{
WriteHead(context, 0);
}
else
{
WriteHead(context, Bytes.Length);
if (Bytes.Length > 0L)
{
var stream = context.Provider.ResponseBody();
stream.Write(Bytes);
}
}
}
#endregion
}
}