2017-07-31 45 views
3

我有一個基本的web api,試圖返回一個作爲下載動作觸發的excel文檔而不是數據流。關於創建流和設置附件屬性,我在網上關注了很多示例,但我的郵差請求始終顯示爲已編碼。以下是API方法。返回任務<IHttpActionResult>中的xlsx數據與EPPlus

[Route("getexcel")] 
[HttpPost] 
public async Task<IHttpActionResult> GetExcel() 
{ 
    IHttpActionResult result = null; 
    FileInfo newFile = new FileInfo(@"C:\Test.xlsx"); 
    ExcelPackage pck = new ExcelPackage(newFile); 

    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); 
    response.Content = new ByteArrayContent(pck.GetAsByteArray()); 
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); 
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); 
    response.Content.Headers.ContentDisposition.FileName = "Test.xlsx"; 

    result = ResponseMessage(response); 
    return result; 
} 

從上面的代碼,如果我使用MediaTypeHeaderValue(「應用/ PDF」),則請求郵差示出下載請求但對於XLSX格式或應用程序/八位字節流沒有。相反,它顯示了一個數據流。 Content-Type是application/json,因爲api的最終版本將在主體中使用json數據。

Postman request

回答

1

我可能會晚點,但是...... 有一個在下拉菜單中選擇「發送和下載」的發送按鈕 enter image description here

+0

這是正確的解決方案,我意識到了這一點,幾天後創建問題,忘記更新,謝謝回覆。 –