webapi返回文件流

2021-06-06 20:03

阅读:672

逻辑说明

webapi返回类型为IHttpActionResult接口,内部方法返回HttpResponseMessage

public interface IHttpActionResult
{
    TaskHttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken);
}

参照JsonResult,自定义返回文件流。
主要为:设置文件响应内容流,文件内容类型,文件名。

HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);
httpResponseMessage.Content = new StreamContent(_stream);
httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(_mediaType);
httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = HttpUtility.UrlEncode(_fileName, Encoding.UTF8),
    };

完整代码

完整FileStreamResult如下:

public class FileStreamResult : IHttpActionResult
{
    readonly Stream _stream;
    readonly string _mediaType;
    readonly string _fileName;

    public FileStreamResult(Stream stream, string mediaType) : this(stream, mediaType, null) { }

    public FileStreamResult(Stream stream, string mediaType, string fileName)
    {
        _stream = stream;
        _mediaType = mediaType;
        _fileName = fileName;
    }

    public TaskHttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        return Task.FromResultHttpResponseMessage>(Execute());
    }

    private HttpResponseMessage Execute()
    {
        HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);
        try
        {
            httpResponseMessage.Content = new StreamContent(_stream);
            httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(_mediaType);
            if (!string.IsNullOrEmpty(_fileName))
            {
                httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = HttpUtility.UrlEncode(_fileName, Encoding.UTF8),
                };
            }
            return httpResponseMessage;
        }
        catch
        {
            httpResponseMessage.Dispose();
            throw;
        }
    }
}

使用方法:

[HttpGet]
public IHttpActionResult Logo()
{
    var path = HostingEnvironment.MapPath("/files/logo.png");
    var f = new FileInfo(path);
    if (!f.Exists)
    {
        return new StatusCodeResult(HttpStatusCode.NotFound, this);
    }
    return new FileStreamResult(f.OpenRead(), "image/png");
}

[HttpGet]
public IHttpActionResult D()
{
    var path = HostingEnvironment.MapPath("/files/d.docx");
    var f = new FileInfo(path);
    if (!f.Exists)
    {
        return new StatusCodeResult(HttpStatusCode.NotFound, this);
    }
    return new FileStreamResult(f.OpenRead(), "application/octet-stream", "中文的jun2019.docx");
}

扩展的FileByteResult

public class FileByteResult : FileStreamResult
{
    public FileByteResult(byte[] buffer, string mediaType) : base(new MemoryStream(buffer), mediaType) { }

    public FileByteResult(byte[] buffer, string mediaType, string fileName) : base(new MemoryStream(buffer), mediaType, fileName) { }
}


评论


亲,登录后才可以留言!