2011-12-12 2 views
5

JSON 데이터 구조를 제공하는 WCF Rest Service 프로젝트를 설정했습니다. WCF REST 서비스에서 비 JSON, 비 XML 데이터 반환

[OperationContract] 
[WebInvoke(Method = "GET", 
    ResponseFormat = WebMessageFormat.Json, 
    BodyStyle = WebMessageBodyStyle.Bare, 
    UriTemplate = "location/{id}")] 
Location GetLocation(string id); 

지금 WebService에 표준 웹 서버가하는 것처럼 멀티미디어 (이미지, PDF 문서) 문서를 반환해야 : I는 인터페이스 파일과 같은에서 계약을 정의했습니다. ResponseFormat의 WCF WebMessageFormat은 JSON 또는 XML 만 지원합니다. 어떻게 파일을 반환하는 인터페이스의 메서드를 정의합니까?

뭔가 같은 : 그래서

[OperationContract] 
[WebInvoke(Method="GET", 
    ResponseFormat = ????? 
    BodyStyle = WebMessageBodyStyle.Bare, 
    UriTemplate = "multimedia/{id}")] 
???? GetMultimedia(string id); 

그 아래 그림과 같이 당신은 당신의 RESTful 서비스에서 파일을 얻을 수있는 ID (10)

+0

이것 좀보세요 : http://stackoverflow.com/questions/2992095/attaching-files-to-wcf-rest-service-responses – pdiddy

+0

감사합니다. 문제가 해결되어 흥미로운 추가 정보가 포함되어 있습니다. – Pierre

답변

3

와 함께 PDF 문서를 반환 wget http://example.com/multimedia/10 :

[WebGet(UriTemplate = "file")] 
     public Stream GetFile() 
     { 
      WebOperationContext.Current.OutgoingResponse.ContentType = "application/txt"; 
      FileStream f = new FileStream("C:\\Test.txt", FileMode.Open); 
      int length = (int)f.Length; 
      WebOperationContext.Current.OutgoingResponse.ContentLength = length; 
      byte[] buffer = new byte[length]; 
      int sum = 0; 
      int count; 
      while((count = f.Read(buffer, sum , length - sum)) > 0) 
      { 
       sum += count; 
      } 
      f.Close(); 
      return new MemoryStream(buffer); 
     } 

IE에서 서비스를 탐색 할 때 응답에 대한 저장 대화 상자가 열려 있어야합니다.

참고 : 서비스에서 반환하는 파일의 적절한 콘텐츠 형식을 설정해야합니다. 위의 예제에서는 텍스트 파일을 반환합니다.

+0

감사합니다. 텍스트 파일의 내용 유형은 대개 "text/plain"입니다. – Pierre

+0

계약은? –

관련 문제