2017-10-10 26 views
0

私は、このカスタムOdata関数をダウンロードpdfデータベースからpdfをダウンロードすることができます。私はOdata命名ファイルの返信エラーメッセージ

1.with 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

試しnullです手動で設定したファイル名:

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; 

Goneステータスコードの代わりにNotFoundを使用することを検討してください(Goneは非常に具体的な意味を持っています)。

+0

申し訳ありません。それでもまだ最初の問題を解決していないダウンロード名 – NinjaDeveloper

+0

ブラウザの応答ヘッダーを確認してください。 Content-Dispositionが適切な価値を持っている場合、問題はWebAPIにはまったく関係しません。 – donMateo

関連する問題