angularTS to PHP文件上传“;“文件为空”;
我遇到了一个问题-我无法将上传的文件(文件似乎是空的)从前端的输入发送到我的php后端,并且我无法访问“文件”中的内容 我的任务:当用户可以选择*.csv文件时,创建一些表单,在服务器上发送此文件,在SQL表中解析和保存*.csv中的数据以及其他内容 dataload.component.html:angularTS to PHP文件上传“;“文件为空”;,php,angular,file,input,angular6,Php,Angular,File,Input,Angular6,我遇到了一个问题-我无法将上传的文件(文件似乎是空的)从前端的输入发送到我的php后端,并且我无法访问“文件”中的内容 我的任务:当用户可以选择*.csv文件时,创建一些表单,在服务器上发送此文件,在SQL表中解析和保存*.csv中的数据以及其他内容 dataload.component.html: <div> <input type="file" (change)="doUpload($event)"/> </div> import { Compone
<div> <input type="file" (change)="doUpload($event)"/> </div>
import { Component } from '@angular/core';
import { HttpService } from '../http.service';
@Component({
selector: 'app-dataload',
templateUrl: './dataload.component.html',
styleUrls: ['./dataload.component.css'],
providers: [HttpService]
})
export class DataloadComponent {
files: any;
constructor(private http: HttpService) { }
doUpload(event){
let target = event.target || event.srcElement;
this.files = target.files;
console.log ('Files: ',this.files);
const apiURL = 'There is my https://testAPI.php';
this.http.post(apiURL, {'action': 'action','data': this.files})
.subscribe(
data => {
console.log("Done. Response JSON: ",data);
},
error => {
console.log('Error: ', error);
}
);
}
}
testAPI.php
<?php
header ('Content-Type: text/html');
header ('Access-Control-Allow-Origin: *');
header ('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
require_once("includes/connection.php");
$datafile = file_get_contents('php://input');
$obj = json_decode($datafile);
$action = $obj->{'action'};
$data = file_get_contents($obj->{'data'});
$retArr = array(
'text' => "Action = ".$action,
'mess' => 'Data = '.$data
);
echo json_encode(array_values($retArr));
?>
结果:
<div> <input type="file" (change)="doUpload($event)"/> </div>
import { Component } from '@angular/core';
import { HttpService } from '../http.service';
@Component({
selector: 'app-dataload',
templateUrl: './dataload.component.html',
styleUrls: ['./dataload.component.css'],
providers: [HttpService]
})
export class DataloadComponent {
files: any;
constructor(private http: HttpService) { }
doUpload(event){
let target = event.target || event.srcElement;
this.files = target.files;
console.log ('Files: ',this.files);
const apiURL = 'There is my https://testAPI.php';
this.http.post(apiURL, {'action': 'action','data': this.files})
.subscribe(
data => {
console.log("Done. Response JSON: ",data);
},
error => {
console.log('Error: ', error);
}
);
}
}
当我在控制台中选择文件(*.csv)时,我看到:
我尝试使用papaparse和ngx papaparse,但我在安装时遇到问题,我尝试在前端解析以转换为JSON,但我无法访问“this.files”中的内容,我尝试在后端解析,但在服务器端也遇到同样的问题
请帮帮我,我很困惑:(您必须使用formData将文件发送到我使用过的服务器,但结果是相同的。
doUpload(event){let target=event.target | | | event.srcelment;this.files=target.files;const formData=new formData();formData.append('file',.files)console.log('files:',this.files);console.log('formData:',formData);const apiURL='有我的https://testAPI.php订阅(数据=>{console.log(“Done.Response JSON:,data”);},错误=>{console.log('error:',error);})}
@PareshGami也许我应该在服务器端做些别的事情?PHP端尝试打印($\u文件)