2016-10-16 97 views
2

客戶端將向我們的web api服務發出GET請求,並且我們需要用指定的文件響應該請求。使用HTTP GET請求從Web Api返回文件內容

文件內容將作爲字節數組,如:

byte[] fileContent = Convert.FromBase64String(retrievedAnnotation.DocumentBody); 

如何迴應能夠拿到上述文件內容爲文件的要求嗎?

我掐滅控制器:

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

    return new object(); 
} 

取而代之的是新的對象,我怎麼返回fileContent的GET請求?

回答

3

您可以從網頁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; 
} 
+0

感謝這麼多,我已經改變了問題,只是有點返回文件內容,即時知道我如何用附件響應,而不在本地保存,我想只是流字節arraay –

+0

也,我不會知道文件類型,即時通訊只會知道擴展的場 –

+0

我得到了這樣的迴應:{ 「FileContents」:「eWVz」, 「 ContentType「:」application/octet-stream「, 」FileDownloadName「:」blabsssg4rrxt.txt「 } –