1

when I upload the file to server, how can get the file name?

I use this method

Map<String, RequestBody> map = new HashMap<>();
            //File file = new File(newvideoPath);
            String fullFilePath = PathUtil.getPathFromUri(this,contentURI);
            // Parsing any Media type file
            File file = new File(fullFilePath);
            RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
            map.put("file\"; filename=\"" + file.getName() + "\"", requestBody);
            ApiConfig getResponse = AppConfig.getApiClient().create(ApiConfig.class);
            Call<ServerResponse> call1 = getResponse.upload("token", map);
            call1.enqueue(new Callback<ServerResponse>() {
                @Override
                public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
                    if (response.isSuccessful()){
                        if (response.body() != null){
                            progressDialog.dismiss();
                            ServerResponse serverResponse = response.body();
                            Toast.makeText(getApplicationContext(), serverResponse.getMessage(), Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(UploadActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }else {

                        Toast.makeText(getApplicationContext(), "problem uploading video", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call<ServerResponse> call, Throwable t) {

                    Log.v("Response gotten is", t.getMessage());
                    Toast.makeText(getApplicationContext(), "problem uploading video " + t.getMessage(), Toast.LENGTH_SHORT).show();

                }
            });

and this is php code

$target_dir = "uploads/";  
$target_file_name = $target_dir .basename($_FILES["file"]["name"]);  
$response = array(); 

$video_path = basename($_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file_name)

I can upload the file to server, but I cannot get the file name

if the file name is 111.MOV, the upload successful, it's can find 111.MOV in uploads forder.

but, how can get the file name?

LAE
  • 23
  • 3
  • 1
    Please get rid of `PathUtil.getPathFromUri()`. Please use the `Uri` properly. You do not need a "real path" to upload a file using OkHttp. See [this](https://stackoverflow.com/q/56308559/115145) and [this](https://commonsware.com/blog/2020/07/05/multipart-upload-okttp-uri.html) for more. – CommonsWare Nov 22 '20 at 12:55
  • If you `var_dump($target_file_name)`, what does it show? – El_Vanja Nov 22 '20 at 12:58
  • return the file name from php or get it in your app itself as you are saving it with the same name – aryanknp Nov 22 '20 at 12:58
  • @CommonsWare I had try that's method, but I don't know I cannot upload file, so I change the PathUtil.getPathFromUri() – LAE Nov 22 '20 at 13:04
  • @El_Vanja has got json error code – LAE Nov 22 '20 at 13:08
  • @aryan agarwal how can I do? – LAE Nov 22 '20 at 13:09

0 Answers0