2017-10-10 41 views
0

我有這個自定義Odata函數從下載pdf數據庫下載pdf。我有一些問題Odata命名文件返回錯誤信息的

1.採用PDF文檔名不名「reportname.pdf」它被命名爲response.pdf

2.return reportBinary的錯誤信息爲空

[HttpGet] 
     [ODataRoute("GetDownloadReport(downloadId={downloadId})")] 
     public HttpResponseMessage GetDownloadReport(Guid downloadId) 

     var received = DateTime.UtcNow; 
     byte[] reportBinary = null; 
     string queryString = "SELECT report FROM downloads WHERE id = @downloadId "; 
     bool success = false; 
     using (SqlConnection conn = new SqlConnection(connectionString)) 
     { 
      //get the binary from database 
     } 

     HttpResponseMessage response = null; 
     try 
     { 

      if (reportBinary == null) 
       return Request.CreateResponse(HttpStatusCode.Gone); 


      response = new HttpResponseMessage(HttpStatusCode.OK); 
      response.Content = new ByteArrayContent(reportBinary); 
      response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { 
       FileName = "PORTName.pdf" 
      }; 
      response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf"); 
      return response; 
     } 
     catch (Exception ex) 
     { 

      return Request.CreateResponse(HttpStatusCode.Gone); 
     } 
} 

回答

1

嘗試手動設置文件名:

String headerInfo = "attachment; filename=" + System.Web.HttpUtility.UrlPathEncode("PORTName.pdf"); 
response.Content.Headers.Add("Content-Disposition", headerInfo); 

我不知道你想做些什麼錯誤信息是什麼,但如果你的意思是設置字符串的內容,只是將其設置;)

response = Request.CreateResponse(HttpStatusCode.Gone); 
response.Content = new StringContent(...); 
return response; 

考慮使用NotFound而不是Gone狀態碼(Gone具有非常明確的含義)。

+0

對不起。但仍然沒有解決第一個問題的下載名稱 – NinjaDeveloper

+0

在瀏覽器中檢查響應標題。如果Content-Disposition具有正確的價值,那麼問題根本與webapi無關。 – donMateo

相關問題