this is going to be a question about sending data from the iPhone to a MVC 3 function.
To get into the topic what I'm already doing and what is working, just to see the style how I'm implementing the connections.
Here I have a sample MVC Controller function, running on my IIS 7.0.
The SampleController : Controller
public ActionResult MyFunction(MyObject object) {
ContentResult result = new ContentResult();
result.Content = some content from the server encoded as JSON
return result;
}
On the iPhone I call this function like this.
NSString *urlString = [NSString stringWithFormat:@"https://%@:%d/%@/%@", _host, _port, Sample, MyFunction];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:json]; // some json encoded object that fits the MyObject from asp.NET
NSURLResponse *response;
NSError *error;
// synchronous so the sample code is shorter
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
// Do some error handling and http status code stuff and finally the json stuff from the NSData object
So this stuff is all working fine, but now I'm at a point where it get's confusing. I need to upload a NSData Object, containing image Data or other stuff. Uploading just the file using mulipart/form-data as shown in this SO answer. But when adding some url parameters it does not work anymore.
____________________________________________________
So now there are two options from here to solve my problem:
The first approach is: How would a NSURLRequest look like for using a Function like this? Or is this even possible?
public ActionResult MyFunction(MyObject object, byte[] binaryData) {
// Some server stuff ect.
}
Or how would I build my NSURLRequest using my first Function and adding a file attachment?
Edit
Here is how I build my current file upload request. It is basically the same I do in the second code snippet.
NSString *urlString = [NSString stringWithFormat:@"https://%@:%d/%@/%@", _host, _port, Sample, MyFunction];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:json];
And finally the file attachment. Note When only using the file attachment without parameters everything works fine, but well yeah the params are missing.
+ (void)attachFile:(NSData *)fileData withFilename:(NSString *)filename toRequest:(NSMutableURLRequest *)request {
NSString *boundary = @"---------------------------94712889831966399282116247425";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", filename]
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:fileData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSData *oldBody = [request HTTPBody];
if (oldBody) {
[body appendData:oldBody];
}
[request setHTTPBody:body];
}
So my guess is now, that there is some boundary stuff for the parameters missing.
Note
As an addition I use a synchronous NSURLConnection on a background Thread.
NSURLResponse *response;
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
NSURLConnection
in asynchronous mode, implementing the delegates. You need that anyway, later when you have to implement your server trust authentication with a self signed cert. – CouchDeveloper Jun 13 at 20:57