It is necessary to transfer several files to the server (1-4 .mp4 videos). How to transfer them separately I know, and whether it is possible to transfer all files at once in one request using Retrofit. Or with the help of other libraries.

  • Look here at stackoverflow.com/a/34792503/3212712 - Yuriy SPb
  • And how did you implement the file selection? For example, I need to select a photo from the gallery and upload it, now I'm sitting with the problem file not found! - Oleg Miroshin
  • @OlegMiroshin, In my case, I have no file selection. I just record the video from the camera and send it. To transfer photos, I usually use Base64. - Nikotin N

2 answers 2

Everything turned out to be much simpler than I thought, it was just that the server did not correctly parse my request, so I decided that I could not transfer several files at once)

So we describe the request for retrofit:

@Multipart @PUT("/upload") Call<ResponseBody> upload(@Header("X-Security-Key") String token, @Part List<MultipartBody.Part> files); 

So do:

  Retrofit retrofit = new Retrofit.Builder() .baseUrl(Constants.BASE_URL) .addConverterFactory(LoganSquareConverterFactory.create()) .build(); final RestAPI service = retrofit.create(RestAPI.class); List<MultipartBody.Part> files = new ArrayList<>(); String path = getOutputMarkStorage() + File.separator; for (int i = 0; i < model.getVideo_list().size(); i++) { File file = new File(path + model.getVideo_list().get(i)); // create RequestBody instance from file RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); // MultipartBody.Part is used to send also the actual file name MultipartBody.Part file_body = MultipartBody.Part.createFormData("video" + i, file.getName(), requestFile); files.add(file_body); } Call<ResponseBody> call = service.upload(context.getSharedPreferences("detail", Context.MODE_PRIVATE).getString(Constants.TOKEN, "null"), files); 

    Interface:

     @Multipart @POST("/upload/index.php") Observable<Void> postImage(@Part MultipartBody.Part image, @Part("name") RequestBody name); 

    Model:

     Observable<Void> postImage(MultipartBody.Part image, RequestBody name); 

    File selection and download:

     RxPhoto.requestUri(context,TypeRequest.GALLERY) .observeOn(AndroidSchedulers.mainThread()) .doOnNext(new Action1<Uri>(){ @Override public void call(Uri uri) { Log.d(tag, "up => " + uri); Log.d(tag, "getPath => " + getPath(context,uri)); String URL = getPath(context,uri); File file = new File( URL+"" ); RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); MultipartBody.Part body = MultipartBody.Part.createFormData("upload", file.getName(), reqFile); RequestBody name = RequestBody.create(MediaType.parse("multipart/form-data"), "upload_test"); GetDataSubscription = model.postImage(body,name) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(); } }) .subscribe(); 

    Auxiliary function for finding the file, there was a snag, I found it here

    php:

     error_reporting(E_ALL); ini_set('display_errors', 1); define('ROOT_DIR', dirname(__FILE__)); $file_path = ROOT_DIR . ""; $file_path = $file_path . basename( $_FILES['upload']['name']); if(move_uploaded_file($_FILES['upload']['tmp_name'], $file_path) ){ echo '{"result": "success"}'; } else{ echo '{"result": "fail"}'; }