Video cannot be uploaded to server side [react native + codeigniter]

518 views Asked by At

I faced this problem since yesterday and I am still unable to find a solution for this. I just don't have any idea why was the video cannot be uploaded by using rest server.

I am using react native document picker with axios to upload files to server side and it's working fine with image but not video. It always return the error

The filetype you are attempting to upload is not allowed.

From my react native - to submit files :

_submitPost(){
  const configuration = {
    headers: {
      "Content-type": "multipart/form-data; charset=UTF-8"
    }
  }

  const data = new FormData();
  if(this.state.fileUri != null){
    data.append('files', {
      uri: this.state.fileUri,
      type: this.state.fileType,
      name: this.state.fileName
    });
  }

  axios.post("api-url-goes-here", data, configuration).then(res=>{
    if(res.data.status == true){
        console.log('uploaded')
    } else {
        console.log('failed to upload')
    }
  })
}

The file type is video/mp4 and the file name is video_20190424_125833.mp4

From CodeIgniter server side

if(isset($_FILES['files']) && !empty($_FILES['files']['name'])){
    $postAttc = $this->upload_files(POSTS_DIR, 'files', 'mp4|3gp|jpg|jpeg|png|gif');
} else {
    $postAttc = null;
}

$this->response([
    'http_status_code' => REST_Controller::HTTP_OK,
    'status' => true,
    'postAttc' => $postAttc,
    'statusMsg' => 'OK'
], REST_Controller::HTTP_OK);

And here is my upload_files() function to upload the file

public function upload_files($upload_path, $fileName, $file_types){
    $config['upload_path']          = $upload_path;
    $config['allowed_types']        = $file_types;
    $config['max_size']             = 0;

    $config['file_name'] = uniqid();

    $this->load->library('upload', $config);
    $this->upload->initialize($config);

    if($this->upload->do_upload($fileName)){
        $uploadData = $this->upload->data();
        $file_name = $uploadData['file_name'];

        return $file_name;
    } else {
        return $this->upload->display_errors();
    }
}

I'm not sure what is wrong with my code because I've tried applying exactly the same code for web version to upload video and it was a success but not with uploading from react native.

0

There are 0 answers