I need to upload image to custom server. I have code like this:
NSString* boundary = @"blablablablabla";
NSString* boundaryString = [NSString stringWithFormat:@"\r\n--%@\r\n", boundary];
NSString* boundaryStringFinal = [NSString stringWithFormat:@"\r\n--%@--\r\n", boundary];
NSMutableData* postData = [NSMutableData dataWithCapacity:[imageData length] + 1024];
NSString* s = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"s\"\r\n\r\n%@\r\n", @"addimage"];
NSString* ad = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"ad\"\r\n\r\n%@\r\n", adIdx];
NSString* u = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"u\"\r\n\r\n%@\r\n", uid];
[postData appendData:[boundaryString dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[s dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[ad dataUsingEncoding:NSUTF8StringEncoding]];
[postData app开发者_开发百科endData:[u dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[boundaryString dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\";\r\nfilename=\"image.jpg\"\r\nContent-Type: image/jpg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:imageData];
[postData appendData:[boundaryStringFinal dataUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http:a.b.c/d.php"]];
[request setHTTPMethod:@"POST"];
NSString* dataLength = [NSString stringWithFormat:@"%d", [postData length]];
[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
[request addValue:@"a.b.c" forHTTPHeaderField:@"Host"];
[request addValue:dataLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
[NSURLConnection connectionWithRequest:request delegate:self];
It looks good but in response for this request I have line like this: "Missing paramter u". Can somebody tell me where is the problem?
The missing parameter u
message is almost certainly coming from the PHP on the server. I presume its supposed to be in one of the "u"\r\n\r\n
constructions. You'd need to know what the server expects to puzzle it out.
I would suggest you dump the post request as a string, paste in a browser and see if you can get it to work. That way, you can make sure that you understand the proper form of the request so you can build it in code.
精彩评论