如何解决如何从服务器中的表单数据获取文件
我无法在服务器上获取文件数据。 _ $ FILE说为空。我做这个槽离子应用程序。我通过http参数发送信息。
cargarFoto(idnegocio,formData) {
let datos = new HttpParams()
.set("proceso","subirImgNegocioApp")
.set("imgRestaurante",formData)
.set("idnegocio",idnegocio)
let headerOptions = new HttpHeaders().set('Content-Type','application/x-www-form-urlencoded');
return this.http.post(this.path,datos,{
observe: 'response',responseType: 'text',headers: headerOptions
});
}
这是我发送表格数据和其他信息的服务代码。 但是当我使用$ _FILES获取文件时,它说是null或为空。我在做什么错?实际上,所有信息都在$ _POST上获得。我将帖子返回到应用程序,显示的内容是
正文:“ { “ proceso”:“ subirImgNegocioApp”, “ imgRestaurante”:“ [对象FormData]”, “ idnegocio”:“ 4” }“
解决方法
我可以建议一些不同的方法-将所有内容作为表单数据发送:
sendSomething(someVar: string,files: File[]) {
const formData = new FormData();
for (const f of files) {
formData.append('file[]',f);
}
formData.append('someVar',someVar);
return this.http.post('/someUrl',formData,{
headers: new HttpHeaders({ enctype: 'multipart/form-data' })
});
}
然后用PHP:
$someVar = $_REQUEST['someVar']
文件(或一个文件)将进入
$_FILES['file']
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。