0

此 WCF 服务返回 TIFF 图像。它检查它是否连接到我们的存储库 - 并从数据文件中获取字节。它检查文件是 PDF、tiff 还是图像,并返回适当的 mime 类型。我现在可以调用该服务,它会返回适当的文件——但图像名称为“documentID”.tif。如何设置它返回的图像的文件名?

[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"));
    }
}
4

2 回答 2

1

不要直接返回 ,而是Stream返回一个自定义对象(例如CustomStream),其中包含Stream以及您要表示的文件的名称Stream

于 2012-03-28T16:13:04.550 回答
1

我发现在 RESTful 服务中执行此操作的最佳方法是使用Content-Disposition标头。大多数浏览器开箱即用地支持此功能,并会弹出一个带有标题建议名称的另存为对话框。至于其他客户端,如果他们注意标题,就会被击中或错过,如果您控制客户端,那么您可以随时添加它。

于 2012-03-28T16:20:12.977 回答