【问题标题】:Download Pdf File with WCF使用 WCF 下载 PDF 文件
【发布时间】:2017-01-14 01:10:01
【问题描述】:

我必须创建一个允许下载 pdf 的 WCF 服务 (.net 4.5),而不是在 REST 模式下。

我是这样定义接口的

[ServiceContract]
public interface IService1
{
    [OperationContract]
    Stream GetPdfFile();
}

并以这种方式实现GetPdfFile

    public Stream GetPdfFile()
    {
        Stream ret = null;
        try
        {
            string downloadFilePath = @"C:\Users\jjkdk\Desktop\WTI_PETERS.pdf";
            string fileName = downloadFilePath.Substring(downloadFilePath.LastIndexOf(@"\") + 1);
            String headerInfo = "attachment; filename=" + fileName;
            WebOperationContext.Current.OutgoingResponse.Headers["Content-Disposition"] = headerInfo;

            WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";

            ret = File.OpenRead(downloadFilePath);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return ret;
    }

app.config 中的 Service 标签如下:

<system.serviceModel>
    <services>
      <service name="WCFAperturaAllegatiCrm.Service1">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8733/Design_Time_Addresses/WCFAperturaAllegatiCrm/Service1/" />
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" contract="WCFAperturaAllegatiCrm.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />          
        </behavior>
      </serviceBehaviors>
    </behaviors>
</system.serviceModel>

然后我创建了一个控制台客户端,运行上面的 WCF,向 url 服务添加一个服务引用,并在控制台客户端中插入以下代码:

SrvAperturaAllegati.Service1Client srv = new SrvAperturaAllegati.Service1Client();
Stream stream = srv.GetPdfFile();
Console.WriteLine();

我收到以下异常:

System.ServiceModel.ProtocolException:内容类型text/html;响应消息的charset=utf->8 与绑定的内容类型不匹配>(application/soap+xml; charset=utf-8)。如果使用自定义编码器,请确保 >IsContentTypeSupported 方法已正确实现。响应的前 1024 个字节是:........

我挣扎着没有结果。

谁能帮帮我?

【问题讨论】:

标签: wcf pdf c#-4.0 utf-8


【解决方案1】:

这个问题来自

....
WebOperationContext.Current.OutgoingResponse.Headers["Content-Disposition"] = headerInfo;
WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";
...

如果您想将此服务用作 SOAP 服务,则不能添加 OutgoingResponse.Headers 和 OutgoingResponse.ContentType。注释这一行,它将在 SOAP 中正常工作。此行用于 REST 服务。

这里更正了代码:

public Stream GetPdfFile()
{
    Stream ret = null;
    try
    {
        string downloadFilePath = @"C:\Users\jjkdk\Desktop\WTI_PETERS.pdf";
        string fileName = downloadFilePath.Substring(downloadFilePath.LastIndexOf(@"\") + 1);
        String headerInfo = "attachment; filename=" + fileName;
        //WebOperationContext.Current.OutgoingResponse.Headers["Content-Disposition"] = headerInfo;

        //WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";

        ret = File.OpenRead(downloadFilePath);
    }
    catch (Exception ex)
    {
        throw ex;
    }
    return ret;
}

【讨论】:

    猜你喜欢
    • 2017-08-14
    • 2016-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多