Android - 如何在一个请求中上传多个文件

时间:2017-06-09 06:31:38

标签: android file-upload

我想在一个请求中上传多个文件。我可以将文件上传到服务器,但我不能上传多个文件。这是我的代码

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

我尝试上传图片文件。这是开始图像选择器

 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

活动结果

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

日志显示此信息

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

我尝试发送这样的多个文件

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

工作正常,但我选择带文件选择器的文件。文件数量将是动态的。上面的代码我得到文件计数静态。用户选择的文件,我的程序必须上传所有文件

1 个答案:

答案 0 :(得分:2)

您可以做的是使MultipartUploadRequest的对象循环并通过您的文件上传,您可以将文件添加到该对象。

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