如何在iphone中将图像和多个数据同步或上传到远程服务器

时间:2011-09-16 11:22:30

标签: iphone objective-c

我正在努力将图像和视频与其他数据一起上传但是没有成功我怎么能这样做。 我的应用程序就像我从sqlite数据库获取数据,我想从sqlite得到的数据。对于图像只有url。 在此之前,我完成了与普通数据的同步。但是图像和视频我不知道怎么做。 为了同步或上传数据,我完成了代码 喜欢这个

-(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) {
        webData = [[NSMutableData data] retain];
        NSLog(@"%@",webData);

    }


}

但如何将多个数据发送到带有图像和视频数据的服务器

1 个答案:

答案 0 :(得分:0)

请尝试将此数据发布到服务器上。

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];