2015-02-11 51 views
3

我得到的問題,我無法從$ HTTP響應

HTTP/1.1 200 OK
的Content-Length頭獲取文件名:121257
內容類型:應用/ PDF
服務器:HTTPAPI/2.0
訪問控制允許來源:*
訪問控制 - 暴露 - 接頭:*
內容處置:附件;文件名=訂單414.pdf
日期:星期三,2015年2月11日5點32分25秒GMT

我只是想下載時獲得的文件名(訂單414.pdf)爲PDF名稱。但在此代碼塊中:

$http.get(httpPath, { responseType: 'arraybuffer' }) 
      .success(function (data, status, headers) { 
       debugger; 
       // just return content-type 
       var header = headers(); 

頭對象只包含內容類型。

Object {content-type: "application/pdf"} 

我讀的地方,我們需要配置用於CORS作爲的WebAPI:

private static void RegisterCorsConfig(HttpConfiguration config) 
    { 
     var cors = new EnableCorsAttribute("*", "*", "*", "*"); 
     //var cors = new EnableCorsAttribute("*", "*", "*", "DataServiceVersion, MaxDataServiceVersion"); 
     //cors.ExposedHeaders.Add("*"); 
     //cors.ExposedHeaders.Add("filename"); 
     config.EnableCors(cors); 
    } 

但它仍然無法正常工作。請幫幫我。 在此先感謝。

回答

7

我想你需要添加Content-Disposition,而不是filenameAccess-Control-Expose-Headers

cors.ExposedHeaders.Add("Content-Disposition"); 
+0

它節省了我的日子,謝謝你 – 2015-02-11 05:52:41

+0

大聽到:)你可以將其標記爲正確。 – Rebornix 2015-02-11 05:54:03

0

的Web API:我發現,添加以下代碼行到我IHttpActionResult實施ExecuteAsync(...)方法的工作(「響應」是要返回HttpResponseMessage):

response.Content.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");