angular2将文件上传到PHP,$ _FILES为空

时间:2017-05-22 18:44:06

标签: php angular

我正在尝试使用angular2和PHP服务器上传文件。变量$ _FILES为空,我无法弄清楚原因。

组件代码: let formData:FormData = new FormData();

for (let i = 0; i < this.filmsFiles.length; i++) {
    formData.append('images', this.filmsFiles[i], this.filmsFiles[i].name);
}


this.filmDataService.uploadFiles(formData).subscribe( outPutData => {
  if(outPutData[0]){...

服务角色代码:

uploadFiles(formData:FormData):Observable {     this.data =&#39; action = 10060&amp; jsonData =&#34;&#34;&#39;;

let headers = new Headers();
headers.append('Content-Type', 'multipart/form-data; charset=utf-8; boundary=' + Math.random().toString().substr(2));
headers.append('Accept', 'application/json');

let options = new RequestOptions({ headers: headers });

return this.http.post(this.url+'?'+this.data, formData, options)
        .map((response:Response) => response.json())
        .catch(this.handleError)

}

PHP代码:

foreach($_FILES['images']['error'] as $key => $error){
        if($error == UPLOAD_ERR_OK){
            $name = $_FILES['images']['name'][$key];
            $fileNameParts = explode(".", $name);
            $nameWithoutExtension = $fileNameParts[0];
            $extension = end($fileNameParts);
            $newFileName = "film_".$i.time().".".$extension;
            move_uploaded_file($_FILES['images']['tmp_name'][$key], '../../images/' . $newFileName);

            $newFileNames[]='images/filmsImages/'.$newFileName;
            $i++;
        }

我一直在检查变量this.filmsFiles是否为空,它包含要上传的文件,在视图中,文件标签,我有:

(变化)=&#34; filmsFiles [I] = $ event.target.files [0]&#34;

服务器接收我发送的参数但在$ _FILES中没有。

你能帮我吗?

谢谢, 罗伯特

0 个答案:

没有答案