2010-02-12 33 views

回答

1

這取決於你想發送到服務器。例如:圖像文件或文本(就像來自http表單)。

對於一個映像文件格式的UIImage:

NSData *imageData = [NSData dataWithContentsOfFile:ImagePath]; 

NSData *imageData = UIImageJPEGRepresentation(myImage.image, 1.0); 

設置你的遠程URL(例如,如果你正在使用PHP)

NSString *urlString = "http://yourdomain.com/yourphpfile.php" 

其餘代碼:

NSMutableURLRequest *request2 = [[[NSMutableURLRequest alloc] init] autorelease]; 
      [request2 setURL:[NSURL URLWithString:urlString]]; 
      [request2 setHTTPMethod:@"POST"]; 

      NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
      NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
      [request2 addValue:contentType forHTTPHeaderField: @"Content-Type"]; 


NSMutableData *body = [NSMutableData data]; 

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  

[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name='userfile'; filename='"] dataUsingEncoding:NSUTF8StringEncoding]]; 
//give any name for your file(this case image file example)   
[body appendData:[[NSString stringWithString:pic_filename.jpg] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[[NSString stringWithString:@"'\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 


//add the file data to the http body    
    [body appendData:[NSData dataWithData:imageData]]; 

[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

// setting the body of the post to the reqeust 
[request2 setHTTPBody:body]; 

只是文本(在PHP類似於$ _GET)

準備要發送數據的情況。對於來自例如

//You have your first name stored in the_first_name_to_send variable 

NSString *f_name = nil; 
f_name = [[NSString alloc] initWithFormat:@"%@", the_first_name_to_send]; 

//Encoding conversion 
//This is optional but it ensures that every character encoding type is sent without issues 
NSString* escapedUrlStringFirstName =[f_name stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; 

NSString *string1 = @"firstName="; 

NSString *myRequestString; 
     myRequestString = [string1 stringByAppendingString:escapedUrlStringFirstName]; 
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ]; 

NSMutableURLRequest *request2 = [[[NSMutableURLRequest alloc] init] autorelease]; 

[request2 setHTTPBody: myRequestData ]; 

[request2 setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 

NSData *returnData2 = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ]; 

returnData2 = nil; 

上傳圖像文件(在服務器端yourphpfile.php)

$file_tmp = $_FILES['userfile']['tmp_name']; 
$filename = $_FILES['userfile']['name']; 

使用HTTP POST發送文本(服務器端yourphpfile.php)

形式的第一個名字
$fname = $_POST["firstName"]; 
+0

感謝您的快速回復。我試圖將10000字節的文本數據發佈到服務器。與上述有點不同嗎? – Ferdinand 2010-02-12 10:47:41

+0

謝謝。我知道了。 – Ferdinand 2010-02-12 11:11:52

+0

應該沒有區別。請注意,發送的數據對於文本和圖像都採用NSData格式。我曾與包含超過500000字節的圖像:)。這些行: [NSData dataWithData:imageData] and [NSData dataWithBytes:[myRequestString UTF8String] length:[myRequestString length]]; – erastusnjuki 2010-02-12 11:17:44