2012-03-28 63 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

到目前为止,这对我有效。 – 2012-03-28 16:30:08

1

而是直接返回Stream的,返回包含Stream的自定义对象(例如CustomStream),以及你要代表Stream文件的名称。

+0

我该怎么做?我可以使用文件流对象吗? – 2012-03-28 16:15:20

+0

创建你自己的班级。它可以包含一个'Stream'以及一个用于文件名的字符串。 – Bernard 2012-03-28 16:18:30