1

I wanted to upload multiple files in one request. I can upload file to server, but I cann't upload more then one file. here is my code

try { String uploadId = UUID.randomUUID().toString(); uploadReceiver.setDelegate((SingleUploadBroadcastReceiver.Delegate) this); uploadReceiver.setUploadID(uploadId); //Creating a multi part request new MultipartUploadRequest(this, uploadId, UPLOAD_URL) .addFileToUpload(path, "p_url[]") .addParameter("phone_number", phone_number) .addHeader("Authorization", "Bearer " + token) //Adding token .setNotificationConfig(new UploadNotificationConfig()) .setMaxRetries(2) .startUpload(); //Starting the upload } catch (Exception exc) { Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show(); } 

I try to upload image files. This is start image picker

 ImagePicker.create(this) .folderMode(true) // set folder mode (false by default) .folderTitle("Folder") // folder selection title .imageTitle("Tap to select") // image selection title .single() // single mode .multi() // multi mode (default mode) .limit(10) // max images can be selected (999 by default) .showCamera(true) // show camera or not (true by default) .imageDirectory("Camera") // captured image directory name ("Camera" folder by default) .origin(images) // original selected images, used in multi mode .start(PICK_FILE_REQUEST); // start image picker activity with request code 

And Activity result

if (requestCode == PICK_FILE_REQUEST && resultCode == RESULT_OK && data != null) { filePath = data.getData(); images = data.getParcelableArrayListExtra(ImagePickerActivity.INTENT_EXTRA_SELECTED_IMAGES); StringBuilder sb = new StringBuilder(); for (int i = 0; i < images.size(); i++) { Log.d("____IMAGES_____", images.get(i).getPath()); } } 

Log show this information

D/____IMAGES_____: /storage/emulated/0/DCIM/ios-logo1.png D/____IMAGES_____: /storage/emulated/0/DCIM/Screenshot_1491908860.png D/____IMAGES_____: /storage/emulated/0/DCIM/product_4.png 

And I try to send multiple file like this

try { String uploadId = UUID.randomUUID().toString(); uploadReceiver.setDelegate((SingleUploadBroadcastReceiver.Delegate) this); uploadReceiver.setUploadID(uploadId); //Creating a multi part request new MultipartUploadRequest(this, uploadId, UPLOAD_URL) .addFileToUpload(path1, "p_url[]") .addFileToUpload(path2, "p_url[]") .addParameter("phone_number", phone_number) .addHeader("Authorization", "Bearer " + token) //Adding token .setNotificationConfig(new UploadNotificationConfig()) .setMaxRetries(2) .startUpload(); //Starting the upload } catch (Exception exc) { Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show(); } 

It is work fine, but I choose files with file chooser. And files count will be dynamical. Above code I get files count static. User selected files, and my programm must to upload all of them

3
  • are u lopping through file count?? Commented Jun 9, 2017 at 6:35
  • sorry I can't read your comment Commented Jun 9, 2017 at 6:37
  • post the full code Commented Jun 9, 2017 at 6:38

1 Answer 1

2

What you can do is make the object of MultipartUploadRequest and looping through your files to upload you can add the files to that object.

MultipartUploadRequest request = new MultipartUploadRequest(this, uploadId, UPLOAD_URL); for(int i=0; i<filesToUploadCount; i++){ //change path and data according to your files request.addFileToUpload(pathForFile, "p_url[i]"); } request.addParameter("phone_number", phone_number); request.addHeader("Authorization", "Bearer " + token); //Adding token request.setNotificationConfig(new UploadNotificationConfig()); request.setMaxRetries(2); request.startUpload(); 
Sign up to request clarification or add additional context in comments.

1 Comment

Can I use HurlStack with this library??

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.