码迷,mamicode.com
首页 > Windows程序 > 详细

webapi返回文件流

时间:2019-04-24 23:43:50      阅读:553      评论:0      收藏:0      [点我收藏+]

标签:file   word   deb   UNC   tps   sha   文件   webapi   ocx   

逻辑说明

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

public interface IHttpActionResult
{
    Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken);
}

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

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 Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        return Task.FromResult<HttpResponseMessage>(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) { }
}

webapi返回文件流

标签:file   word   deb   UNC   tps   sha   文件   webapi   ocx   

原文地址:https://www.cnblogs.com/junio/p/10765632.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!