i have copied all address book contacts to an array and write it to a plist(xml file) file in applications documents directory. how can i upload this file to a web server? please give me some reference about this topics.
Asked
Active
Viewed 6,715 times
2 Answers
1
Here is a similar question: File Upload to HTTP server in iphone programming
Code:
NSString *urlString = @"http://yourserver.com/upload.php";
NSString *filename = @"filename";
request= [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:YOUR_NSDATA_HERE]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(returnString);
-
where does this code is taking the pah of my file? and what is boundary? i don't understand it. please explain the code... – Shahriar Apr 20 '11 at 09:40
-
[postbody appendData:[NSData dataWithData:YOUR_NSDATA_HERE]]; Instead of "YOUR_NSDATA_HERE" put "[NSData dataWithContentOfFile:@"path/to/file"]" The code is not from me, follow my link to get more infos ;) – ingham Apr 20 '11 at 20:55