2014-03-27 13 views
-1

我正在使用NSURLConnection來整合Web服務。發佈圖片使用iphone中的參數

我的代碼如下:

NSURL *url = [NSURL URLWithString:Sign_Up_URL]; 
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; 
    NSString *params = [NSString stringWithFormat:@"vimage=%@&@"vFname=%@&vLname=%@&vEmail=%@&vPassword=%@&vDeviceType=%@&vDeviceToken=%@",image_data,txt_first.text,txt_last.text,txt_email.text,txt_password.text,@"IOS",@"safasf423526dgdsgdsg"]; 
    [urlRequest setHTTPMethod:@"POST"]; 
    [urlRequest setValue:header_filed forHTTPHeaderField:@"X_API_KEY"]; 
    [urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]]; 

    [NSURLConnection sendAsynchronousRequest:urlRequest 
    queue:[NSOperationQueue mainQueue] 
    completionHandler:^(NSURLResponse *response, 
    NSData *data, 
    NSError *connectionError) { 
    // handle response 
    [MBProgressHUD hideHUDForView:self.view animated:YES]; 
    NSDictionary *json_response = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 
    NSLog(@"%@",json_response); 
    // [self login_response:json_response]; 
    }]; 

但我怎麼能上傳圖片用這個參數?

Also Refer this

說明這一點。

+0

你將不得不在NSData中轉換你的圖像,然後你必須用你的參數上傳這些數據。 – NiravPatel

+0

@NiravPatel,謝謝你的回覆!我已經嘗試轉換NSdata,但沒有得到成功的結果。 – user2893370

+0

請參閱http://stackoverflow.com/questions/11084403/uploading-image-via-post-in-objective-c解決方案。可能是有用的.. – Vidhyanand

回答

0

請閱讀文章:http://nthn.me/posts/2012/objc-multipart-forms.html和使用的ContentType:的multipart /從數據;

,供大家參考:

NSString *boundary = @"YOUR_BOUNDARY_STRING"; 
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 stringWithFormat:@"Content-Disposition: form-data; name=\"photo\"; filename=\"%@.jpg\"\r\n", self.message.photoKey] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"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]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"message\"\r\n\r\n%@", self.message.message] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user\"\r\n\r\n%d", 1] dataUsingEncoding:NSUTF8StringEncoding]]; 

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

[request setHTTPBody:body]; 

希望這會有所幫助,請更新我,如果你得到任何錯誤。

0

通過使用NSURLConnection的,你可以做類似下面

NSData* fileData = [[NSData alloc] initWithContentsOfFile:image_data]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setURL:[NSURL URLWithString:Sign_Up_URL]]; 
[request setHTTPMethod:@"POST"]; 
  
NSString *boundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"]; // This is important! //NSURLConnection is very sensitive to format. 
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",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"VFname\"; filename=\"thefilename\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[NSData dataWithData:fileData]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"param2\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:self.emailtextfield.text] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"param3\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:self.passwordtextfield.text] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:self.vlnametextfield.text] dataUsingEncoding:NSUTF8StringEncoding]]; 
// setting the body of the post to the reqeust 
[request setHTTPBody:body]; 

(或使用ASIHTTPRequest你可以如下做到這一點)

ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:Sign_Up_URL]; 
    [request setDelegate:self]; 

    [request setPostValue:vnamelabel.textforKey:@"VFname"]; 

    [request setPostValue:emaillabel.text forKey:@"vEmail"]; 

    [request setPostValue:passwordlabel.text forKey:@"vPassword"]; 

    [request setPostValue:devicetypelabel.text forKey:@"vDeviceType"]; 

    [request addData:self.imagedata withFileName:@"image.jpeg" andContentType:@"image/jpeg" forKey:@"vimage"]; 
    //---------------------- 

希望它可以幫助你..