发布表单字段 +使用xhr文件,并重建服务器端
我正在尝试通过 XHR 请求将 HTML 表单的数据提交到 php 脚本。
我遇到了代码问题,但是我怀疑这与后端有关,但是可能在 javascript 中做了一些不正确的事情,如下所示:
var fd = new FormData();
fd.append("title", $('#uploadVidTitle').val());
fd.append("proj", $('#uploadVidProject').val());
fd.append("desc", $('#uploadVidDesc').val());
fd.append("action", $('#uploadAction').val());
console.log(fd);
fd.append("uploadFile", document.getElementById('videoUpload').files[0]);
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
xhr.open("POST", "actions.php", false);
xhr.send(fd);
目前在 php 代码中我正在获取表单通过 $_POST["name"]
方法获取文件内容,但是无法使用 $_FILES["name"]
检索文件内容。任何指示将不胜感激。
I'm trying to submit an HTML form's data to a php script via an XHR request.
I've hit a problem with the code however which I suspect has something to do with the back-end side of things, however might be doing something incorrect in javascript which is as follows:
var fd = new FormData();
fd.append("title", $('#uploadVidTitle').val());
fd.append("proj", $('#uploadVidProject').val());
fd.append("desc", $('#uploadVidDesc').val());
fd.append("action", $('#uploadAction').val());
console.log(fd);
fd.append("uploadFile", document.getElementById('videoUpload').files[0]);
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
xhr.open("POST", "actions.php", false);
xhr.send(fd);
Currently in the php code I'm getting the forms contents via the $_POST["name"]
method, however cannot retrieve the files contents using $_FILES["name"]
. Any pointers would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题出在服务器端。 Ty this
Retriving file names by using $_FILES[$id]['name'] 对我有用。
I think problem at server side. Ty this
Retrieving file names by using $_FILES[$id]['name'] worked for me.
您可能想在发送请求之前尝试设置一些请求标头:
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
或
xhr.setRequestHeader("Content-type", "multipart/form-data");
发送 POST 请求后还要检查 $_FILES 超全局,以查看是否可以从中访问任何文件。
You might want to try setting some request headers before sending the request:
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
or
xhr.setRequestHeader("Content-type", "multipart/form-data");
Also inspect the $_FILES superglobal after POST request is sent to see if any files can be accessed from it.