2010-06-09 40 views
-1

我有這個代碼我在youtube上找到xD我的問題是,我該如何添加一個額外的參數?Uploadin Image - Objective C

NSData *imageData = UIImageJPEGRepresentation(imagen.image,0.0); 
NSString *urlString = @"http://urlexample.com/upload.php"; 
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:urlString]]; 
[request setHTTPMethod:@"POST"]; 

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 
[request 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=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[NSData dataWithData:imageData]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[request setHTTPBody:body]; 

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

NSLog(returnString); 

我嘗試

[body appendData:[[NSString stringWithFormat:@"extra=%@",info] dataUsingEncoding:NSUTF8StringEncoding];

呵呵呵IM 100%,新上這個,你可以看到。請幫我由xD

+1

能否請您格式化碼。它對我來說根本不可讀 – vodkhang 2010-06-09 03:17:19

+0

:) Man im 100%noob on objective-c我只是在YouTube上找到該代碼 – Sammaeliv 2010-06-09 03:31:09

+0

是否要將參數添加到URL? – 2010-06-09 03:40:50

回答

1

我最近碰到this earlier thread來了,我覺得你要尋找的是這樣的添加一個字段名爲foo並設置它吧:

[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"foo\"\r\n\r\nbar\n"]]]; 
+0

是someting這樣的,但由於某種原因沒有工作,我做了一個print_r($ _ POST)在PHP文件nslog響應和showme數組( ); – Sammaeliv 2010-06-09 19:28:22