从ASP.NET Web API中的控制器返回二进制文件

Jos*_*arl 313 asp.net asp.net-mvc asp.net-web-api

我正在使用ASP.NET MVC的新的WebAPI的网络服务,将成为了二进制文件,主要是.cab.exe文件.

以下控制器方法似乎有效,这意味着它返回一个文件,但它将内容类型设置为application/json:

public HttpResponseMessage<Stream> Post(string version, string environment, string filetype)
{
    var path = @"C:\Temp\test.exe";
    var stream = new FileStream(path, FileMode.Open);
    return new HttpResponseMessage<Stream>(stream, new MediaTypeHeaderValue("application/octet-stream"));
}
Run Code Online (Sandbox Code Playgroud)

有一个更好的方法吗?

car*_*ira 501

尝试使用简单HttpResponseMessageContent属性设置为StreamContent:

// using System.IO;
// using System.Net.Http;
// using System.Net.Http.Headers;

public HttpResponseMessage Post(string version, string environment,
    string filetype)
{
    var path = @"C:\Temp\test.exe";
    HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
    var stream = new FileStream(path, FileMode.Open, FileAccess.Read);
    result.Content = new StreamContent(stream);
    result.Content.Headers.ContentType = 
        new MediaTypeHeaderValue("application/octet-stream");
    return result;
}
Run Code Online (Sandbox Code Playgroud)

关于stream使用的一些注意事项:

  • 您不能调用stream.Dispose(),因为Web API在处理控制器方法时仍需要能够访问它result以将数据发送回客户端.因此,不要使用using (var stream = …)块.Web API将为您配置流.

  • 确保流的当前位置设置为0(即流的数据的开头).在上面的例子中,这是给定的,因为你刚刚打开了文件.但是,在其他情况下(例如,当您首次将一些二进制数据写入a时MemoryStream),请确保stream.Seek(0, SeekOrigin.Begin);或设置stream.Position = 0;

  • 使用文件流,显式指定FileAccess.Read权限可以帮助防止Web服务器上的访问权限问题; IIS应用程序池帐户通常只具有对wwwroot的读/列/执行访问权限.

  • 史蒂夫 - 你是对的,我通过向FileStream.Dispose添加断点并运行此代码来验证.框架调用HttpResponseMessage.Dispose,它调用StreamContent.Dispose,调用FileStream.Dispose. (41认同)
  • 您是否恰好知道流何时关闭?我假设框架最终调用HttpResponseMessage.Dispose(),它反过来调用HttpResponseMessage.Content.Dispose()有效地关闭流. (37认同)
  • 你不能在结果(`HttpResponseMessage`)或流本身中添加`using`,因为它们仍将在方法之外使用.正如@Dan所提到的,在完成向客户端发送响应之后,它们会被框架处理掉. (15认同)
  • @carlosfigueira,嗨,你知道如何在字节全部发送后删除文件吗? (5认同)
  • @ B.ClayShannon是的,就是这样.就客户端而言,它只是HTTP响应内容中的一堆字节.客户端可以使用他们选择的那些字节,包括将其保存到本地文件. (2认同)
  • 还要尝试添加以下行-&gt; result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue(“ attachment”){FileName =“ test.exe”}; (2认同)

Ron*_*rby 133

对于Web API 2,您可以实现IHttpActionResult.这是我的:

using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

class FileResult : IHttpActionResult
{
    private readonly string _filePath;
    private readonly string _contentType;

    public FileResult(string filePath, string contentType = null)
    {
        if (filePath == null) throw new ArgumentNullException("filePath");

        _filePath = filePath;
        _contentType = contentType;
    }

    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        var response = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new StreamContent(File.OpenRead(_filePath))
        };

        var contentType = _contentType ?? MimeMapping.GetMimeMapping(Path.GetExtension(_filePath));
        response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);

        return Task.FromResult(response);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在你的控制器中这样的事情:

[Route("Images/{*imagePath}")]
public IHttpActionResult GetImage(string imagePath)
{
    var serverPath = Path.Combine(_rootPath, imagePath);
    var fileInfo = new FileInfo(serverPath);

    return !fileInfo.Exists
        ? (IHttpActionResult) NotFound()
        : new FileResult(fileInfo.FullName);
}
Run Code Online (Sandbox Code Playgroud)

这里有一种方法可以告诉IIS忽略具有扩展名的请求,以便请求将其发送给控制器:

<!-- web.config -->
<system.webServer>
  <modules runAllManagedModulesForAllRequests="true"/>
Run Code Online (Sandbox Code Playgroud)

  • 对于那些正在运行这个运行IIS7 +的人来说,这是一个难题.runAllManagedModulesForAllRequests现在可以[省略](https://www.iis.net/configreference/system.webserver/modules). (4认同)
  • 实际上已经有一个内置的 FileResult(甚至 FileStreamResult)类。 (2认同)

Eri*_*dil 8

虽然建议的解决方案工作正常,但还有另一种方法从控制器返回一个字节数组,响应流格式正确:

  • 在请求中,设置标题"Accept:application/octet-stream".
  • 在服务器端,添加媒体类型格式化程序以支持此mime类型.

不幸的是,WebApi不包括"application/octet-stream"的任何格式化程序.GitHub上有一个实现:BinaryMediaTypeFormatter(有一些小的改编使它适用于webapi 2,方法签名改变了).

您可以将此格式化程序添加到全局配置中:

HttpConfiguration config;
// ...
config.Formatters.Add(new BinaryMediaTypeFormatter(false));
Run Code Online (Sandbox Code Playgroud)

BinaryMediaTypeFormatter如果请求指定了正确的Accept标头,WebApi现在应该使用.

我更喜欢这个解决方案,因为返回byte []的动作控制器更容易测试.但是,如果您想要返回另一种内容类型而不是"application/octet-stream"(例如"image/gif"),则另一种解决方案允许您进行更多控制.


JBA*_*JBA 7

对于在使用接受的答案中的方法下载相当大的文件时多次调用API问题的任何人,请将响应缓冲设置为true System.Web.HttpContext.Current.Response.Buffer = true;

这可确保在将二进制内容发送到客户端之前在服务器端缓冲整个二进制内容.否则,您将看到多个请求被发送到控制器,如果您没有正确处理它,该文件将被损坏.

  • `Buffer`属性[已被弃用](https://msdn.microsoft.com/en-us/library/system.web.httpresponse.buffer(v = vs.110).aspx)支持`BufferOutput` .它默认为"true". (2认同)

KMJ*_*sen 7

对于使用.NET Core的用户:

您可以在API控制器方法中使用IActionResult接口,如下所示:

    [HttpGet("GetReportData/{year}")]
    public async Task<IActionResult> GetReportData(int year)
    {
        // Render Excel document in memory and return as Byte[]
        Byte[] file = await this._reportDao.RenderReportAsExcel(year);

        return File(file, "application/vnd.openxmlformats", "fileName.xlsx");
    }
Run Code Online (Sandbox Code Playgroud)

此示例已简化,但应该理解这一点。在.NET核心这个过程是这样比在.NET之前的版本更加简单-即没有设置响应类型,内容,标题等。

另外,当然,文件和扩展名的MIME类型将取决于个人需求。

参考:SO发帖人@NKosi

  • 请注意,如果它是图像并且您希望可以在具有直接 URL 访问的浏览器中查看它,则不要提供文件名。 (5认同)

Dav*_*den 6

您正在使用的重载设置序列化格式化程序的枚举.您需要明确指定内容类型,如:

httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
Run Code Online (Sandbox Code Playgroud)

  • 谢谢回复.我尝试了这个,我仍然在Fiddler中看到`Content Type:application/json`.如果我在返回`httpResponseMessage`响应之前中断,则"内容类型"似乎设置正确.还有什么想法吗? (2认同)

Mic*_*mig 5

你可以尝试

httpResponseMessage.Content.Headers.Add("Content-Type", "application/octet-stream");
Run Code Online (Sandbox Code Playgroud)