Asp.net web api 如何从ASP.net 5 web api返回文件

Asp.net web api 如何从ASP.net 5 web api返回文件,asp.net-web-api,asp.net-core,postman,Asp.net Web Api,Asp.net Core,Postman,在asp.net 5中创建wep api。我试图返回Post请求的文件响应。但是响应不是文件,而是 ` 代码: public HttpResponseMessage Post([FromBody]DocumentViewModel vm) { try { if (ModelState.IsValid) { var Document = _repository.GetD

在asp.net 5中创建wep api。我试图返回Post请求的文件响应。但是响应不是文件,而是 `

代码:

    public HttpResponseMessage Post([FromBody]DocumentViewModel vm)
    {
        try
        {
            if (ModelState.IsValid)
            {

                var Document = _repository.GetDocumentByGuid(vm.DocumentGuid, User.Identity.Name);
                var Params = Helper.ClientInputToRealValues(vm.Parameters, Document.DataFields);
                var file = Helper.GeneratePdf(Helper.InsertValues(Params, Document.Content));                 

                var result = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new ByteArrayContent(System.IO.File.ReadAllBytes(file))
                };
                result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
                {
                    FileName = "test.pdf"
                };
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
                return result;

            }

        }
        catch (Exception ex)
        {
            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return null;
        }
        Response.StatusCode = (int)HttpStatusCode.BadRequest;
        return null;

    }

如何将真实文件作为响应而不是JSON返回?我使用Postman作为测试客户端

设置
HttpContext.Response.ContentType=“application/pdf”
有帮助吗

这应该符合您的需要:

public FileResult TestDownload()
    {
        HttpContext.Response.ContentType = "application/pdf";
        FileContentResult result = new FileContentResult(System.IO.File.ReadAllBytes("YOUR PATH TO PDF"), "application/pdf")
        {
            FileDownloadName = "test.pdf"
        };

        return result;                                
    }

使用IActionResult而不是HttpResponseMessage。并返回FileStreamResult,并使其正常工作

遇到新问题,该文件不是我使用服务器中的流打开的文件。但这将产生一个新的问题

继续:

谢谢

这是一种“低级”HTTP方法,它应该同时适用于ASP.NET WebAPI或ASP.NET MVC

[HttpGet]
public HttpResponseMessage Download()
{
  var fs = new FileStream(myfileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, 32768, true);
  var response = new HttpResponseMessage {
    Content = new StreamContent(fs);
  }
  response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
  response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
  return response;
}

也许这会有所帮助:不,它仍然返回json作为结果:
“headers”:[],
现在填充了什么吗?正如我从你最初的帖子中看到的,它是空的。您使用的是asp.net 5的哪个版本,它是完整的.net还是coreclr?不,它仍然是空的。full-dnx451,build rc1 finalIf如果我手动将标题添加到HttpResponseMessage中,如下所示:result.Headers.add(“内容类型”,“应用程序/pdf”);它抛出:“误用了头名称。请确保请求头与HttpRequestMessage一起使用,响应头与HttpResponseMessage一起使用,内容头与HttpContent对象一起使用。”在WebAPI有机会使用文件流之前,
fs.Dispose()
是否会关闭文件?如果没有,那么
Dispose()
在这种情况下如何工作?回答我上面的问题:您不需要(也可能不应该)在传递给
StreamContent
的流上调用
Dispose
。我会编辑你的答案。请看,它仍然不是WebAPI,因为WebAPI控制器中不存在
this.Request.CreateResponse
[HttpGet]
public HttpResponseMessage Download()
{
  var fs = new FileStream(myfileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, 32768, true);
  var response = new HttpResponseMessage {
    Content = new StreamContent(fs);
  }
  response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
  response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
  return response;
}