首页 文章

改造 - 将视频上传到XAMPP

提问于
浏览
0

我是Retrofit的新手,在过去的两天里遇到了很大的问题 . 我想从我的设备相机发送视频到XAMPP服务器 .

php部分应该移动上传的视频:

$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;

接口:

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);

码:

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());
        }
    });

当我使用密钥“filename”通过Postman发送视频文件时,php服务器部分工作,如move_uploaded_file($ _ FILES [“filename”] [“tmp_name”] .

我尝试了不同的例子,这个特别是来自https://futurestud.io/tutorials/retrofit-2-how-to-upload-files-to-server我成功了 .

问题是,日志中没有错误 . 但我确切地知道,一旦它到达move_uploaded_file就会出现问题($ _ FILES [“filename”] [“tmp_name”]

1 回答

  • 0

    好的,我终于找到了问题和解决方案 . 首先,我的XAMPP上的目录 videos 是只读的 . 我正在使用Mac并通过“获取信息”将文件夹的“共享和权限”属性更改为 Read and Write

    其次,我发现了一种将密钥文件对映射到我的php代码的方法,"filename"是 move_uploaded_file($_FILES["filename"]["tmp_name"] 中的密钥:接口:

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

    码:

    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
    

相关问题