how to sync or upload the image and multiple data on to the remote server in iphone
I am trying so much to upload the image and video with other data But not success how can I do that . My app is like that i have take data from the sqlite database and I want to that data which i am gettig from the sqlite .And for image have only the url . Before that I done sync with the normal data .But image and video i have no idea how can do that . For sync or uploading data i done code Like this
-(void)sendRequest
{
NSDate* date = [NSDate date];
//Create the dateformatter object
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//Set the required date format
[formatter setDateFormat:@"dd-MMM-yyyy"];
//Get the string date
NSString* str = [formatter stringFromDate:date];
NSString *post = [NSString stringWithFormat:@"Token=%@&LocationID=%@&JourneyID=%@&Altitude=%@&Longitude=%@&Latitude=%@&Speed=%@&Accuracy=%@&LocationDate=%@&LastSyncDate=%@",tokenapi,Location_ID,Journey_jlID,Altitude_jl,Longitude_jl,Latitude_jl,Speed_jl,Accuracy_jl,LocationDate_jl,str];
NSLog(@"%@",post);
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSLog(@"%@",postLength);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init]autorelease];
[request setURL:[NSURL URLWithString:@"http://www.google.com?RequestType=UserJourneyLocation&Command=NEW"]];
NSLog(@"%@",request);
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
开发者_C百科 webData = [[NSMutableData data] retain];
NSLog(@"%@",webData);
}
}
But how can send the multiple data to server with image and video data
Please try this to post the data on server.
NSError *error = nil;
NSHTTPURLResponse *response = nil;
NSURL *url = [NSURL URLWithString:POST_URL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *boundary = @"-------------------a9d8vyb89089dy70";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setHTTPMethod:@"POST"];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-disposition: form-data; name=\"contentID\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"127_8_1315579702522" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
......
// Pass here your other Paramater as like Content ID
......
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-disposition: form-data; name=\"image_file\"; filename=\"%@\"\r\n",@"image.jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:ImageDataPost];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postBody];
NSData *shoutData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
精彩评论