Hi I am uploading image on php server and successfully uploaded with the help of this code
- (NSString*)uploadData:(NSData *)data toURL:(NSString *)urlStr:(NSString *)_userID
{
// File upload code adapted from http://www.cocoadev.com/index.pl?HTTPFileUpload
// and http://www.cocoadev.com/index.pl?HTTPFileUploadSample
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlStr]];
[request setHTTPMethod:@"POST"];
// NSString* = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
NSString *stringBoundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[[NSString stringWithFormat:@"userId=%@",_userID] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"image%@.jpg\"\r\n", _userID] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);
return returnString;
}
Now i want to send my user Id also on derver side how can i send my user id? I tried to it in this way
[body appendData:[[NSString stringWithFormat:@"userId=%@",_userID] dataUsingEncoding:NSUTF8StringEncoding]];
but in vain then i tried to send it in this way
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"image%@.jpg\"; userId=\"%@\"\r\n", _userID, _userID] dataUsingEncoding:NSUTF8StringEncoding]];
but again in vain so kindly guide me what to do!
If you haven’t thought about using AFNetworking, you should. It makes dealing with web-services much easier. I have done something similar to what I think you are wanting to do. I wrote a custom class to fire off network requests. Here ya go:
NetworkClient.h
NetworkClient.m
Some of this is specific to what I do. All my web services use an APIKey to enhance security and they all do security checks before processing any requests. The network client will return either an NSDictionary or an NSArray depending on what the web service returns (in JSON).
To use the image upload, you would call this in your VC like this (use as many parameters as you need.:
Feel free to strip out what you don’t need. Just leave the copyright notice in place.