2012-03-28 2 views
0

이 WCF 서비스는 TIFF 이미지를 반환합니다. 저장소에 연결되어 있는지 확인하고 데이터 파일에서 바이트를 가져옵니다. 파일이 PDF, tiff 또는 이미지인지 확인하고 적절한 MIME 유형을 리턴합니다. 이제 서비스를 호출 할 수 있으며 적절한 파일을 반환하지만 이미지 이름은 "documentID".tif입니다. 반환하는 이미지의 파일 이름은 어떻게 설정합니까?이 WCF 서비스는 TIFF 이미지를 반환합니다. 반환하는 이미지의 파일 이름은 어떻게 설정합니까?

[OperationContract] 
[WebInvoke(Method = "GET", UriTemplate="File/{documentID}")] 
Stream GetDocumentFile_GET(string documentID); 




public Stream GetDocumentFile_GET(string documentID) 
{ 
    if (ProprietaryClass.IsConnected) 
    { 
     ProprietaryClass _documentForViewer = new ProprietaryClass(documentID); 
     string _fileType = ProprietaryClass.NativeFileType; 
     string _mimetype = "image/tiff"; 

     switch (_fileType) 
     { 
      case "TIF": 
       _mimetype = "image/tiff"; 
       break; 
      case "PDF": 
       _mimetype = "application/pdf"; 
       break; 
      case "PNG": 
       _mimetype = "image/png"; 
       break; 
     }; 

     if (ProprietaryClass.ProprietaryMethod(_documentForViewer)) 
     { 

      ProprietaryClass _downloadToViewer = new ProprietaryClass(); 

      if (_documentForViewer.TiffFile != null) 
      { 
       _downloadToViewer = _documentForViewer.TiffFile; 
      } 
      else 
      { 
       _downloadToViewer = _documentForViewer.NativeFile; 
      } 


      MemoryStream fileStream = new MemoryStream(_downloadToViewer.FileData); 

      // fileStream is now array of bytes 
      System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse.ContentType = _mimetype; 

      return (Stream)fileStream; 
     } 
     else 
     { 
      return new MemoryStream(Encoding.UTF8.GetBytes("Document type not supported by native viewer")); 
     } 
    } 
    else 
    { 
     return new MemoryStream(Encoding.UTF8.GetBytes("Not connected")); 
    } 
} 

답변

1

RESTful 서비스에서이 작업을 수행 한 가장 좋은 방법은 Content-Disposition 헤더를 사용하는 것입니다. 대부분의 브라우저는 기본적으로이 기능을 지원하며 헤더에 제시된 이름으로 저장 대화 상자를 표시합니다. 다른 클라이언트의 경우 헤더에주의를 기울이면 히트 또는 누락됩니다. 클라이언트를 제어하면 항상 추가 할 수 있습니다.

+0

이것은 지금까지 나를 위해 일했다. –

1

대신 직접 Stream을 반환하는 당신이 Stream을 대표 할 파일의 이름뿐만 아니라 Stream를 포함하는 사용자 지정 개체 (예를 들어, CustomStream)을 반환합니다.

+0

어떻게하면됩니까? 파일 스트림 개체를 사용할 수 있습니까? –

+0

나만의 수업을 만드십시오. 그것은 파일 이름을위한 문자열뿐만 아니라'Stream'을 포함 할 수 있습니다. – Bernard

관련 문제