上传使用FORMDATA多个文件() [英] Uploading multiple files using formData()

查看:1856
本文介绍了上传使用FORMDATA多个文件()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var fd = new FormData();
fd.append("fileToUpload", document.getElementById('fileToUpload').files[0]);
var xhr = new XMLHttpRequest();
xhr.open("POST", "uph.php");
xhr.send(fd);

uph.php:

uph.php:

var_dump($_FILES['fileToUpload']);

这工作,但很明显的文件[0] 而已。如何获取对所选文件的工作?

This works, but obviously for the files[0] only. How to get this working for chosen file?

我试着删除 [0] ,但没有奏效。

I tried removing the [0], but it didn't work.

推荐答案

您必须得到文件长度JS追加,然后通过AJAX请求发送如下

You have to get the files length to append in JS and then send it via AJAX request as below

//JavaScript 
var ins = document.getElementById('fileToUpload').files.length;
for (var x = 0; x < ins; x++) {
    fd.append("fileToUpload[]", document.getElementById('fileToUpload').files[x]);
}

//PHP
$count = count($_FILES['fileToUpload']['name']);
for ($i = 0; $i < $count; $i++) {
    echo 'Name: '.$_FILES['fileToUpload']['name'][$i].'<br/>';
}

这篇关于上传使用FORMDATA多个文件()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆