【问题标题】:This WCF Service returns a TIFF image - how do I set the filename of the image it returns?此 WCF 服务返回 TIFF 图像 - 如何设置它返回的图像的文件名?
【发布时间】:2012-03-28 15:53:05
【问题描述】:

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

【问题讨论】:

    标签: wcf http-streaming


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我该怎么做?我可以使用文件流对象吗?
      • 创建自己的班级。它可以包含Stream 以及文件名字符串。
      猜你喜欢
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-08
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      相关资源
      最近更新 更多