2017-10-10 2 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

시도 널 수동으로 설정 파일 이름 :

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

관련 문제