Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'System.InvalidOperationException: Request format is invalid: multipart/form-data' error when posting image from iphone to .NET webservice

I'm trying to post an image from an iphone app to a .Net webservice and I'm running into this error. I've already updated my web.config as per this kb article and I can successfully post to methods that take strings as params. My issue is attempting to post data with an image. I've tried posting this way and that way, but both ways I wind up with the same error:

System.InvalidOperationException: Request format is invalid: multipart/form-data; charset=utf-8; boundary=0xKhTmLbOuNdArY. at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

Here's my webservice signature:


[WebMethod]
public XmlDocument UploadImageToServer(string usertoken, byte[] image)
{ 
   //stuff happens in here
}

..and here's my most recent attempt:


- (void)sendImageToServer:(NSURL *)serivceURL withUserToken:(NSString *)usertoken
{
 NSData *imageData = UIImageJPEGRepresentation(self.selectedImage, 1.0f);

 ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:serviceURL] autorelease];
 [request setPostValue:usertoken forKey:@"usertoken"];
 [request setData:imageData forKey:@"image"];
 [request setDelegate:self];
 [request startAsynchronous];

 NSLog(@"We set the request out!");

}

In addition, I increased my httpRuntime max request length to 40MB (<httpRuntime maxRequestLength="40960"/>) just to make sure the issue wasn't with the size of my image, but the error persists.

Any help would be appreciated.

like image 511
Abel Avatar asked Jan 14 '10 21:01

Abel


2 Answers

I got this to work by moving these to lines in web.config:

<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

They where placed in the bottom of the <system.webServer><handlers> section in web.config, but since I moved them to the top it seems to work!

like image 69
Fredrik Avatar answered Sep 23 '22 10:09

Fredrik


You are trying to read binary data as input parameters. You need to instead read the parameters from Context.Request. In other words, remove userToken and image as Request Parameters. userToken should be instead accessed through Context.Request["userToken"] and image should be available through Context.Request.PostedFiles["image"]: http://www.rahulsingla.com/blog/2010/07/ext-net-ajax-file-upload-using-web-service

like image 30
r_honey Avatar answered Sep 21 '22 10:09

r_honey