2016-04-07 4 views
-1

これで私はマルチフォームデータを作成しようとしました。しかし、私のデータが適切でないために内部エラーが発生しました。iOS:マルチパートデータ - フォームエラー

私は `Content-Disposition:form-data;名前=「データ」私のパラメータ私は自分のコード

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); 

    } 
}]; 

答えて

0

である私はAfNetworkingのマルチパートを使用してサーバに画像をアップロードしており、コードは以下のとおりであるかdo.Hereに知っていないデータに、

- (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); 
    }]; 
} 

希望します。

0

キー「データ」が欠落していた問題を修正しました。

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