从昨天开始我就遇到了这个问题,但我仍然无法找到解决方案。我只是不知道为什么无法使用休息服务器上传视频。
我正在使用带有axios的react native 文档选择器将文件上传到服务器端,它适用于图像但不适用于视频。它总是返回错误
不允许您尝试上传的文件类型。
从我的本机反应 - 提交文件:
_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')
}
})
}
文件类型是video/mp4
,文件名是video_20190424_125833.mp4
从 CodeIgniter 服务器端
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);
这是我的 upload_files() 函数来上传文件
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();
}
}
我不确定我的代码有什么问题,因为我已经尝试将完全相同的代码应用于 Web 版本来上传视频并且它是成功的,但不是从 React Native 上传。