upload video to XAMPP

I'm new to Retrofit and have a huge problem for the past 2 days. I want to send a video from my device camera to XAMPP server.

php part where the uploaded video should be moved:

$returnArray = array();
$videoUploadResult = "";

$target_dir ="/Applications/XAMPP/xamppfiles/htdocs/Projects/Eventtest/videos";
if(!file_exists($target_dir)) {
   mkdir($target_dir, 0777, true);
}

$target_file_name = $target_dir . "/" . basename($_FILES["filename"]["name"]);


if(move_uploaded_file($_FILES["filename"]["tmp_name"], $target_file_name)) {
    $returnArray["video_upload_status"] = "Video uploaded successfully";
} else {
    $returnArray["status"] = 400;
    $returnArray["message"] = "Couldn't upload the video";

    echo json_encode($returnArray);
}
exit;

Interface:

public interface ServerInterface {
   @GET("getEvents.php")
   Call<List<JSONData>> getEvent(@Query("result") String tag);

   @POST("createEvent.php")
   //@FormUrlEncoded
   @Multipart
   Call<ResponseBody> uploadVideo(@Part("description") RequestBody description, @Part MultipartBody.Part file); 

Code:

ServerInterface = APIClient.getClient().create(ServerInterface.class);


    RequestBody requestFile =
            RequestBody.create(
                    MediaType.parse("video/mp4"),
                    videoFile
            );
    MultipartBody.Part body =
            MultipartBody.Part.createFormData("filename", videoFile.getName(), requestFile);

    // add another part within the multipart request
    String descriptionString = "hello, this is description speaking";
    RequestBody description =
            RequestBody.create(
                    MultipartBody.FORM, descriptionString);

    // finally, execute the request
    Call<ResponseBody> call = serverInterface.uploadVideo(description, body);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call,
                               Response<ResponseBody> response) {
            Log.v("Upload", "success");
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.e("Upload error:", t.getMessage());
        }
    });

The php server part works when I send a video file through Postman with the key "filename", as in move_uploaded_file($_FILES["filename"]["tmp_name"].

I've tried deferent examples, this particular is from https://futurestud.io/tutorials/retrofit-2-how-to-upload-files-to-server I've also tried to send a Map with String and file, but didn't succeeded.

The problem is, there is no error in the logs. But I know exactly, that the problem occurs as soon as it reaches move_uploaded_file($_FILES["filename"]["tmp_name"]


Ok, I finally found the problem and the solution. First of all, the directory videos on my XAMPP was Read-only. I'm using Mac and changed the Sharing and Permissions properties of the folder to Read and Write through the Get Info

Secondly, I found a way to map the key-file pair to my php code, "filename" is my key in move_uploaded_file($_FILES["filename"]["tmp_name"] : Interface:

@POST("createEvent.php")
@Multipart
Call<ResponseBody> uploadVideo(@Part MultipartBody.Part file, @Part("filename") RequestBody name);

Code:

    serverInterface = APIClient.getClient().create(ServerInterface.class);
    RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), videoFile);
    MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("filename", videoFile.getName(), requestBody);
    RequestBody filename = RequestBody.create(MediaType.parse("text/plain"), videoFile.getName());


    Call<ResponseBody> call = serverInterface.uploadVideo(fileToUpload, filename);
call.enqueue(......) // onResponse(), onFailure() goes here
enter code here
链接地址: http://www.djcxy.com/p/75394.html

上一篇: 从多部分/表格读取文件输入

下一篇: 将视频上传到XAMPP