将 multipart/form-data 发布到 WCF REST 服务:操作更改 [英] POSTing multipart/form-data to a WCF REST service: the action changes

查看:63
本文介绍了将 multipart/form-data 发布到 WCF REST 服务:操作更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WCF Rest 服务:

I have a WCF Rest service:

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

我想上传文件(好吧,文件;一次一个).使用此表单进行测试:

That I want to upload files (well, file; one at a time) to. Using this form to test:

    <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,我什么也得不到;该服务永远不会受到打击.我中断了代码以查看发生了什么,但从未达到过.我需要在 URITemplate 属性中为 'multipart/form-data' enctype 做一些特别的事情吗?我还缺少什么?最重要的是,我使用 Fiddler 查看每个选项发送到服务的内容,并没有什么险恶的外观.

I can see the service receive the data, even though with the enctype of the form being set to 'multipart/form-data', only receive the element name and the name of the uploaded file. However, if I set the enctype, I get nothing; the service is never hit. I put a break in the code to see what was going on, and it was never reached. Do I need to do something special in the URITemplate attribute for the 'multipart/form-data' enctype? What else am I missing? For the heck of it, I used Fiddler to see what was being sent to the service with each option, and there was nothing sinister looking.

没有multipart/form-data":

Without '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

我没有想法

推荐答案

你看到这个问题了吗?WCF 服务接受后编码的 multipart/form-data

这篇关于将 multipart/form-data 发布到 WCF REST 服务:操作更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆