4

Thanks in advance... I need help in uploading file (image file) to server using Retrofit2 library. I have already acheived simple (text based) request and response. But i am facing issue in uploading image file to server. Below is my Android Code:

Upload Function

Map<String, RequestBody> map = new HashMap<>(); File file = new File(mediaPath); RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file); map.put("file\"; filename=\"" + file.getName() + "\"", requestBody); ApiConfig getResponse = AppConfig.getRetrofit().create(ApiConfig.class); Call<ServerResponse> call = getResponse.upload("token", map); call.enqueue(new Callback<ServerResponse>() { @Override public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) { ServerResponse serverResponse = response.body(); if (serverResponse != null) { if (serverResponse.getSuccess()) { Toast.makeText(getApplicationContext(), serverResponse.getMessage(),Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), serverResponse.getMessage(),Toast.LENGTH_SHORT).show(); } Log.e("Retro", serverResponse.getMessage()); } else { Log.v("Response", serverResponse.toString()); } progressDialog.dismiss(); } @Override public void onFailure(Call<ServerResponse> call, Throwable t) { } }); 

Interface

@Multipart @POST("laundryapp/upload_image.php") Call<ServerResponse> upload( @Header("Authorization") String authorization, @PartMap Map<String, RequestBody> map ); 

ServerResponse File

public class ServerResponse { @SerializedName("success") boolean success; @SerializedName("message") String message; public String getMessage() { return message; } public boolean getSuccess() { return success; } } 

My PHP Code on server

 <?php $target_dir = "uploads/"; $target_dir = $target_dir .basename($_FILES["file"]["name"]); $response = array(); // Check if image file is a actual image or fake image if (isset($_FILES["file"])) { if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir)) { $success = true; $message = "Successfully Uploaded"; } else { $success = false; $message = "Error while uploading ". $target_dir; } } else { $success = false; $message = "Required Field Missing"; } $response["success"] = $success; $response["message"] = $message; echo json_encode($response); ?> 

My Problem

The main problem i am facing is that i always get

Error while Uploading

from server. I have checked the variable $target_dir and it contains the name of image file i am trying to upload. But in actual no file is uploading to my uploads folder... Please any help in this matter. I am exhausted too much now

4
  • 1
    futurestud.io/blog/retrofit-2-how-to-upload-files-to-server Commented Jun 21, 2016 at 12:26
  • this blog may be helps you to upload file on the server Commented Jun 21, 2016 at 12:26
  • Have you checked the write permission on that folder on your server Commented Jun 21, 2016 at 12:29
  • sometimes this is the thing which doesnot catch our eyes Commented Jun 21, 2016 at 12:30

1 Answer 1

4

After searching and helping suggestions from people, I came to know that I have not granted the permission to my file and folder. So after granting permission my code works perfectly fine. This code is correct so anybody can use this code in future... If need some help regarding this code then feel free to ask me. And thanks alot everyone who helped me to solve my problem. Have a nice day...!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.