POSTing multipart/form-data to a WCF REST service: the action changes [duplicate]
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();
arr开发者_运维技巧ayVal = 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();" />
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.
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
With:
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
I'm out of ideas
Change must be made in web.config...
system.serviceModel > bindings > webHttpBinding > binding > transferMode="Streamed"
Did you see this question? WCF service to accept a post encoded multipart/form-data
I have used the above code with no success. My web service is not being called. I have also modified my web.config
according to: "WCF Service to accept a post-encoded multipart form data".
All of my other service methods are working fine.
精彩评论