2

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.

Shahriar
  • 318
  • 3
  • 12

2 Answers2

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);
Community
  • 1
  • 1
ingham
  • 1,636
  • 15
  • 30
  • 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
1

Hi Try following links in which you can find your answer
link1
link2
link3

Community
  • 1
  • 1
dks1725
  • 1,631
  • 1
  • 20
  • 29