,可以通过以下步骤完成:
import { HttpClient } from '@angular/common/http';
...
constructor(private http: HttpClient) {}
submitFile(file: File) {
const formData = new FormData();
formData.append('file', file);
return this.http.post('http://example.com/api/upload', formData);
}
<?php
require APPPATH.'/libraries/REST_Controller.php';
class FileUpload extends REST_Controller {
public function upload_post() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if ($this->upload->do_upload('file')) {
$data = $this->upload->data();
$file_url = base_url('uploads/'.$data['file_name']);
$this->response(['status' => 'success', 'file_url' => $file_url], REST_Controller::HTTP_OK);
} else {
$this->response(['status' => 'error', 'error_message' => $this->upload->display_errors()], REST_Controller::HTTP_BAD_REQUEST);
}
}
}
application/config/routes.php
文件中添加以下代码:$route['api/upload'] = 'fileupload/upload';
以上是将文件从HttpClient Angular提交到CodeIgniter -Rest API 3的完整步骤和代码示例。通过这种方式,你可以在Angular前端应用中选择文件并将其传递给CodeIgniter后端应用进行处理。在实际应用中,你可以根据具体需求进行参数配置、错误处理和文件管理等操作。
领取专属 10元无门槛券
手把手带您无忧上云