2012-08-14 3 views
5

여러 파일을 업로드하는 방법 나는 어떻게 AsyncHttpClient 안드로이드

http://loopj.com/android-async-http/

File myFile = new File("/path/to/file.png"); 
RequestParams params = new RequestParams(); 
try { 
    params.put("profile_picture", myFile); 
} catch(FileNotFoundException e) {} 

AsyncHttpClient

에서 하나의 파일을 업로드 할 수 있습니다 알고하지만 다중 게시물 서버에 여러 파일을 업로드해야합니다. 어떻게하면됩니까?

답변

1

SimpleMultipartEntity 개체를 만들고 업로드 할 각 파일에 대해 addPart를 호출하십시오.

public static void fileUpLoad(String url,File file,AsyncHttpResponseHandler asyncHttpResponseHandler){ 
    RequestParams requestParams=new RequestParams(); 

    try{ 
     requestParams.put("profile_picture", file,"application/octet-stream"); 
     client.post(url, requestParams, new AsyncHttpResponseHandler()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
+0

이 .. 저도 같은 키에 대한 두 개의 파일을 추가 할 –

+1

.. 그냥 처음에 두 번째 파일을 병합 ... 작동하지 않았다 –

+0

이이 방법을 병합 이유가 될 수있다 그걸 고칠 수 있니? 제발 공유 할 –

-1

이 코드를 사용해야합니다. 동적 배열을 원하는 경우, 당신은 방법 .toArray()

와 [] 유형을 파일로 ArrayList에 를 사용하여 변환 할 수 있습니다,

File[] myFiles = { new File("pic.jpg"), new File("pic1.jpg") }; 
RequestParams params = new RequestParams(); 
try { 
    params.put("profile_picture[]", myFiles); 
} catch(FileNotFoundException e) { 

} 

Aditionally : 그렇게하기 위해서는 , 아래의 코드를 따라

ArrayList<File> fileArrayList = new ArrayList<>(); 

//...add File objects to fileArrayList 

File[] files = new File[fileArrayList.size()]; 
fileArrayList.toArray(files); 

희망 도움말. = D

+0

요청은 여러 부분으로 업로드됩니다. –

3

당신은 파일을 키 값으로 파일 배열을 전달할 수 있습니다

+0

여기에 문제가 있습니다. 위와 똑같지 만, OnSuccess CallBack이 호출되지 않습니다. 7 개의 파일 [이미지]를 업로드하면 이미지가 하나만 남고 나머지는 비어 있습니다. 그러나이 파일은 4 개 파일로 작동하지만 성공을위한 콜백은 절대로받지 못합니다. –

0

모든 파일을 매개 변수로 params에 전달해야합니다. 예를 들어

params.put("file_one", myFiles1); 
params.put("file_two", myFiles2) 
0

파일 [] = lst.toArray 파일 (신규 파일 [lst.size()]);

try { 
     params.put("media[]", files); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    }