This WCF Service returns a TIFF image - how do I set the filename of the image it returns?

StackOverflow https://stackoverflow.com/questions/9911263

  •  27-05-2021
  •  | 
  •  

This WCF Service returns a TIFF image. It checks if it is connected to our repository - and gets the bytes from the datafile. It checks if the file is a PDF, tiff, or image, and returns the appropriate mime type. I can now call the service and it returns the appropriate file - but with an image name of "documentID".tif. How do I set the filename of the image it returns?

[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"));
    }
}
有帮助吗?

解决方案

The best way I've found to do this in RESTful services is using the Content-Disposition header. Most browsers support this out of the box and will pop a save as dialog with the name the header suggests. As for other clients it's hit or miss on if they pay attention to the header, if you control the client then you can always add it.

其他提示

Instead of returning the Stream directly, return a custom object (e.g. CustomStream) containing the Stream as well as the name of the file you want to represent the Stream.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top