【问题标题】:POSTing multipart/form-data to a WCF REST service: the action changes [duplicate]将 multipart/form-data 发布到 WCF REST 服务:操作更改 [重复]
【发布时间】:2010-06-02 17:08:41
【问题描述】:

我有 WCF Rest 服务:

      [WebHelp(Comment = "Sample description for GetData")]
    [WebInvoke(Method="POST", UriTemplate = "invoke", BodyStyle =WebMessageBodyStyle.Bare)]
    [OperationContract]
    public string GetData( Stream input)
    {
        long incomingLength = WebOperationContext.Current.IncomingRequest.ContentLength;
        string[] result = new string[incomingLength];
        int cnter = 0;
        int arrayVal = -1;
        do
        {
            if (arrayVal != -1) result[cnter++] = Convert.ToChar(arrayVal).ToString();

            arrayVal = input.ReadByte();
        } while (arrayVal != -1);

        return incomingLength.ToString();
    }

我想将文件(嗯,文件;一次一个)上传到。使用此表格进行测试:

    <form method="post" action="Service.svc/invoke" >
        <input type="file" name="aFile" />
    </form>
    <input type="button" onclick="document.forms[0].submit();" />

我可以看到服务接收数据,即使表单的 enctype 设置为“multipart/form-data”,也只接收元素名称和上传文件的名称。但是,如果我设置 enctype,我什么也得不到;该服务永远不会受到影响。我在代码中放了一个中断以查看发生了什么,但从未达到。我是否需要在“multipart/form-data”编码类型的 URITemplate 属性中做一些特别的事情?我还缺少什么?最糟糕的是,我使用 Fiddler 来查看每个选项发送到服务的内容,并没有什么险恶的外观。

没有'multipart/form-data':

POST /Service4/Service.svc/invoke HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*
Referer: <my machine>/Service4/form.htm
Accept-Language: en-US
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; MS-RTC LM 8; .NET CLR 1.1.4322)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: okrd14144
Content-Length: 89
Connection: Keep-Alive
Pragma: no-cache

与:

POST /Service4/Service.svc/invoke HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*
Referer: <my machine>/Service4/form.htm
Accept-Language: en-US
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; MS-RTC LM 8; .NET CLR 1.1.4322)
Content-Type: multipart/form-data; boundary=---------------------------7da1ce3aa0bd0
Accept-Encoding: gzip, deflate
Host: okrd14144
Content-Length: 150119
Connection: Keep-Alive
Pragma: no-cache

我没有想法

【问题讨论】:

    标签: c# wcf rest


    【解决方案1】:

    必须在 web.config 中进行更改...

    system.serviceModel > bindings > webHttpBinding > binding > transferMode="Streamed"
    

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 我做到了,但是我对WCF的理解很薄弱,所以我可能已经看到了答案而没有意识到。我会回去再看一遍。谢谢。
      • 解决了问题;需要将传输类型设置为“流式传输”。
      • 您在哪里将传输类型更改为流式传输?我遇到了同样的问题。
      • 您在您的webHttpEndpoint 内的standardEndpoint 的web.config 文件中设置传输类型(准确的说是transferMode)。
      【解决方案3】:

      我使用上面的代码没有成功。我的网络服务没有被调用。我还根据:"WCF Service to accept a post-encoded multipart form data"修改了我的web.config

      我所有的其他服务方法都运行良好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-06
        • 2016-11-14
        • 1970-01-01
        相关资源
        最近更新 更多