2013-04-08 3 views
1

iOS 응용 프로그램을 개발 중입니다. 이 응용 프로그램은 하나의 요청으로 단일 및 복수 파일 업로드 옵션을 제공합니다. AFNetworking을 사용하여 단일 파일을 업로드하고 어느 것이 정상적으로 작동하는지 확인하십시오. 이제 여러 파일 업로드를 지원해야합니다. 나는 웹에서 여러 파일 업로드를 실제로하는 HTML 코드를 가지고있다. iOS 앱에서도 동일한 작업을 수행해야한다. 아무도 우리가 AFNetworking을 사용하여 어떻게 동일한 작업을 수행 할 것을 제안 할 수 있습니까? 코드 아래AFNetworking Form Request (하나의 요청으로 여러 파일 업로드)

<form method="post" action="upload.php?key=ac2a04cc7b6d4420fa5e5eb1701fb0cc1acf7916&channelid=1" enctype="multipart/form-data"> 
<label>Title:</label> <input name="title" value="My Video Title" /><br /> 
<label>Date:</label> <input name="date" value="<?php echo time(); ?>" /><br /> 
<label>Location:</label> <input name="location" value="Sydney, Australia" /><br /> 
<textarea name="description">The description of the photo/video goes here</textarea><br /> 
<input name="file[]" type="file" multiple="multiple" /><br /> 

+0

업데이트 된 코드를 확인하십시오. –

답변

8

사용 양식에 여러 파일을 보낼 수 있습니다. 필요에 따라 변경하여 필요한 사항을 변경하십시오. 내가 test.php 코드를 검사 한 서버 측에서

NSArray *imageArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"close_button.png"],[UIImage imageNamed:@"done_button.png"], nil]; 
    __block int i=1; 
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:@"yoururl"]; 
    NSMutableURLRequest *request=nil; 
     request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"yoururl" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) 
     { 
      for(UIImage *eachImage in imageArray) 
      { 
       NSData *imageData = UIImagePNGRepresentation(eachImage); 
       [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"file%d",i] fileName:[NSString stringWithFormat:@"abc%d.png",i] mimeType:@"image/png"]; 
       i++; 
      } 

     }]; 

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; 

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) 
    { 
     NSData *data = (NSData *)responseObject; 
     NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
     NSLog(@"Response -> %@",str); 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) 
    { 
     NSLog(@"Error -> %@",[error localizedDescription]); 
    }]; 

, 나는 단지 인 print_r ($ _ FILES)를 인쇄하고는 추측을 유지하기 미안 내 경우에는 다음과 같은 인쇄합니다.

Response -> Array 
(
    [file1] => Array 
     (
      [name] => abc1.png 
      [type] => image/png 
      [tmp_name] => /tmp/phpiWbxSn 
      [error] => 0 
      [size] => 1997 
     ) 

    [file2] => Array 
     (
      [name] => abc2.png 
      [type] => image/png 
      [tmp_name] => /tmp/phpwtWTE8 
      [error] => 0 
      [size] => 1847 
     ) 
) 

희망이 있습니다.

+0

'NSMutableURLRequest'는 루프 외부에서 액세스 할 수 없습니다. – nsgulliver

+0

답장을 보내 주신 고맙습니다. 귀하의 코드에 문제가 있습니다. for 루프 내에서 요청 값을 다른 값으로 지정하십시오. 이렇게하면 previos 값이 손실됩니다. 요청은 최종 이미지의 멀티 파트 요청 만 가리키기 때문에 마지막 이미지 만 업로드됩니다. –

+0

이미지를 메모리에로드하는 것이 극도로 비용이 많이 듭니다. 향상된 성능을 위해 [appendPartWithFileURL : name : error] (http://afnetworking.github.io/AFNetworking/Protocols/AFMultipartFormData.html#//api/name/appendPartWithFileURL:name:error)를 확인하십시오. – MishieMoo

0

AFHTTPNetwrok 솔루션을 시도했지만 작동하지 않았습니다. 다음과 같이 "name"키에 대괄호를 추가하고 마지막으로 이미지를 성공적으로 업로드합니다. 문제는 무엇일까요?

NSData *imageData = UIImageJPEGRepresentation(img,1); 
      [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"images[%d]",i] fileName:[NSString stringWithFormat:@"images%d.jpg",i] mimeType:@"image/jpeg"]; 
관련 문제