发布表单字段 +使用xhr文件,并重建服务器端

发布于 2024-11-24 20:11:24 字数 869 浏览 0 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拒绝两难 2024-12-01 20:11:24

我认为问题出在服务器端。 Ty this

foreach ($_FILES as $id => $val)
      if (isset($_FILES[$id]['name']) && $_FILES[$id]['name'] != '')
      {
          //process files

Retriving file names by using $_FILES[$id]['name'] 对我有用。

I think problem at server side. Ty this

foreach ($_FILES as $id => $val)
      if (isset($_FILES[$id]['name']) && $_FILES[$id]['name'] != '')
      {
          //process files

Retrieving file names by using $_FILES[$id]['name'] worked for me.

走野 2024-12-01 20:11:24

您可能想在发送请求之前尝试设置一些请求标头:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文