2016-04-07 23 views
-1

Bu işlemde, Çoklu form verileri yapmaya çalışıyorum.Ancak verilerim uygun olmadığından bir iç hata aldım.iOS: Çok satırlı veri biçimi hatası

"Content-Disposition: form-data; = "Veri" adını verilere benim parametre ben do.Here nasıl bilmiyorum benim kod

NSString *stringUrl [email protected]"http://URL"; 
// NSString *mimetype = @"image/jpeg"; 

UIImage *img =[UIImage imageNamed:@"1418954654.png"]; 
NSData *imageDataa = UIImagePNGRepresentation(img); 
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"name",@"first_name",@"name2",@"last_name",@"10-03-2356",@"dob",@"[email protected]",@"email",@"12452",@"facebook_id",@"M",@"gender",@"123456",@"password",@"12345689",@"phone_no",@"U",@"user_type", nil]; 
// 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:stringUrl]]; 
// NSData *imageData = UIImageJPEGRepresentation(image, 1.0); 

[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; 
[request setHTTPShouldHandleCookies:NO]; 
[request setTimeoutInterval:60]; 
[request setHTTPMethod:@"POST"]; 
NSString *boundary = @"unique-consistent-string"; 

// set Content-Type in HTTP header 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@ ;", boundary]; 
[request setValue:contentType forHTTPHeaderField: @"Content-Type"]; 

// post body 
NSMutableData *body = [NSMutableData data]; 
[parameters enumerateKeysAndObjectsUsingBlock:^(NSString *parameterKey, NSString *parameterValue, BOOL *stop) { 
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n,", parameterKey] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", parameterValue] dataUsingEncoding:NSUTF8StringEncoding]]; 
}]; 


if (imageDataa) { 
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=imageName.jpg\r\n", @"photo"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:imageDataa]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    // NSLog(@"bodu%@",body); 
} 

[body appendData:[[NSString stringWithFormat:@"--%@d--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
// NSLog(@"bodu%@",body); 

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

// set the content-length 
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]]; 
NSLog(@"bodyy%@",postLength); 

[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 


[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
    if(data.length > 0) 
    { 
     //success 
     NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
     NSLog(@"==%@",newStr); 
    } 
    else 
    { 

     NSLog(@"==%@",error.localizedDescription); 

    } 
}]; 

cevap

0

Ben AfNetworking multipart kullanarak sunucuya resmi yükleyin ve kod aşağıda verilmiştir olduğu

- (void)uploadPhoto:(NSString *)member withUserImageData:(NSData*)imageData completion:(WebServiceStatusCompletionBlock)completionBlock { 

    NSDictionary *parameters = @{ 
           memberVal: member, 
           }; 

    [self.httpRequestOperationManager POST:URL parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
     if(imageData) 
     { 
      [formData appendPartWithFileData:imageData name:@"Photo" fileName:@"photo.jpg" mimeType:@"image/jpeg"]; 
     } 

    } success:^(AFHTTPRequestOperation *operation, id responseObject) { 

     if (responseObject) { 
      completionBlock(nil, responseObject); 
     } else { 
      completionBlock(responseObject, nil); 
     } 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

//   completionBlock(error, nil); 

     completionBlock(error, operation.responseObject); 
    }]; 
} 

Yararlı olmasını umuyorum.

0

Önemli "verileri" özledim sorunları giderildi.

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