C# 使用HTTP GET请求从Web Api返回文件内容

C# 使用HTTP GET请求从Web Api返回文件内容,c#,asp.net,.net,rest,asp.net-web-api,C#,Asp.net,.net,Rest,Asp.net Web Api,客户端将向我们的web api服务发出GET请求,我们需要使用指定的文件响应该请求 文件内容将以字节数组的形式显示,如: byte[] fileContent = Convert.FromBase64String(retrievedAnnotation.DocumentBody); 如何将上述文件内容作为文件响应GET请求? 我已经删除了一个控制器: [Route("Note({noteGuid:guid})/attachment", Name = "GetAttachment")] [Htt

客户端将向我们的web api服务发出GET请求,我们需要使用指定的文件响应该请求

文件内容将以字节数组的形式显示,如:

byte[] fileContent = Convert.FromBase64String(retrievedAnnotation.DocumentBody);
如何将上述文件内容作为文件响应GET请求?

我已经删除了一个控制器:

[Route("Note({noteGuid:guid})/attachment", Name = "GetAttachment")]
[HttpGet]
public async Task<object> GetAttachment(Guid noteGuid)
{

    return new object();
}
[路由(“注意({noteGuid:guid})/attachment”,Name=“GetAttachment”)]
[HttpGet]
公共异步任务GetAttachment(Guid noteGuid)
{
返回新对象();
}

如何将文件内容返回到GET请求而不是新对象?

您可以使用以下方法从web api返回文件内容

public HttpResponseMessage GetAttachment(Guid noteGuid)
{
    byte[] content = Convert.FromBase64String(retrievedAnnotation.DocumentBody);
    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
    response.Content = new ByteArrayContent(content);
    response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
    response.Content.Headers.ContentDisposition.FileName = "fileName.txt";
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");

    return response;
}

非常感谢,我已经稍微改变了这个问题,我想知道如何在不保存附件的情况下用附件来响应,我只想流式传输那个字节arrayalso,我不想知道文件类型,我只想知道文件的扩展名,我得到了这个响应:{“FileContents”:“eWVz”,“ContentType”:“application/octet stream”,“FileDownloadName”:“blabsssg4rrxt.txt”}FileDownloadName是正确的,但是filecontents应该是“yes”,我不明白,你能说得更清楚吗?