Reputation: 111
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
Upvotes: 4
Views: 1618
Reputation: 111
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...!
Upvotes: 4