所有文件都不是使用 jquery 从文件上传控件获取的
All files are not getting from file upload control using jquery
我在上传图片时遇到问题。
我的窗体中有一个输入类型文件控件,可以使用 jquery 添加更多输入控件。问题是这样的:当我获取所有这些控件值时,它只返回文件控件中的第一个值。
如何在所有控件中获取所有添加的文件?这是我的代码:
爪哇语
$(document).ready(function() {
$('#add_more').click(function() {
$(this).before($("<div/>", {
id: 'filediv'
}).fadeIn('slow').append(
$("<input/>", {
name: 'file[]',
type: 'file',
id: 'file',
accept: '.jpg, .jpeg, .gif'
}),
$("<br/><br/>")
));
});
$('#upload').click(function(e) {
var name = $(":file").val();
if (!name) {
alert("File Must Be Selected");
e.preventDefault();
} else {
//upload all images
var fileData = $('#file').prop("files")[0];
var form_data = new FormData();
form_data.append('file', fileData);
$.ajax({
url: "myurl.php",
dataType: 'text',
data: form_data,
cache: false,
contentType: false,
processData: false,
type: "post",
error: function() {
alert('error');
},
success: function(ret) {
alert('sucess');
}
});
}
}
});
.HTML
<form enctype="multipart/form-data" action="" method="post">
<hr/>
<div id="filediv">
<input name="file[]" type="file" id="file" accept=".jpg, .gif, .jpeg" />
</div>
<br/>
<input type="hidden" value="" id="class" name="class">
<input type="button" id="add_more" class="upload" value="Add More Files" />
<input type="button" value="Upload File" name="submit" id="upload" class="upload" />
</form>
当我使用 php 从 php 获得帖子时$_FILES['file']['name']
并使用 count($_FILES['file']['name'])
它返回1
.
当我进一步处理时,只有第一个文件上传到相应的文件夹中。
第 var fileData = $('#file').prop("files")[0];
行可能有问题.
你的JS代码应该如下:
$(document).ready(function () {
$('#add_more').click(function () {
$(this).before($("<div/>", {
id: 'filediv'
}).fadeIn('slow').append(
$("<input/>", {
name: 'file[]',
type: 'file',
// Id must be unique
// id: 'file',
class: "file_input",
accept: '.jpg, .jpeg, .gif'
}),
$("<br/><br/>")
));
});
$('#upload').click(function (e) {
var boolAreAllFilesSelected = true;
var objFormData = new FormData();
$.each($(":file"), function ( ) {
if (!$(this).val())
{
alert("File Must Be Selected");
$(this).focus();
return boolAreAllFilesSelected = false;
}
else
{
objFormData.append('file[]', $(this).prop("files")[0]);
}
});
if (boolAreAllFilesSelected)
{
$.ajax({
url: "myurl.php",
dataType: 'text',
data: objFormData,
cache: false,
contentType: false,
processData: false,
type: "post",
error: function () {
alert('error');
},
success: function (ret) {
alert('sucess');
}
});
}
});
});
您的PHP代码应如下所示:
<?php
function uploadSingleImage($arrSingleFile = array())
{
if (!empty($arrSingleFile))
{
// Here your image uploading code
}
}
if (!empty($_FILES['file']))
{
$arrFile = $_FILES['file'];
foreach ($arrFile['name'] as $intIndex => $strName)
{
$arrSingleFile["name"] = $strName;
$arrSingleFile["type"] = $arrFile['type'][$intIndex];
$arrSingleFile["tmp_name"] = $arrFile['tmp_name'][$intIndex];
$arrSingleFile["error"] = $arrFile['error'][$intIndex];
$arrSingleFile["size"] = $arrFile['size'][$intIndex];
uploadSingleImage($arrSingleFile);
}
}
else
{
die("You have not uploaded any file.");
}
相关文章:
- 通过ajax到php文件获取单选按钮值
- jquery自动完成从本地文件获取数据
- 从外部js文件获取变量
- 应用程序脚本-自动从谷歌驱动器删除超过3天的文件-获取文件列表
- Ajax请求从php文件获取数据
- 从Javascript文件获取值
- 如何从外部JavaScript文件获取函数参数
- AngularJS从JSON本地文件获取数据
- 如何从另一个文件获取 HTML 元素值
- 从外部 json 文件获取数据
- 从HTML页面执行外部.js文件,通过发送输入.xml并从外部文件获取输出.xml.js文件
- 将数据从单独的 JavaScript 文件获取到数组中
- jQuery Ajax 文件获取问题
- AJAX/Jquery - 从 php 文件获取响应
- 根据 JSON 文件获取对象的架构
- 如何从输入类型=文件获取数据,修改它,然后将其插入到此输入JS中
- HTML和Javascript:使用输入文件获取完整的目录路径名
- 从本地文件获取JSON的角度服务不起作用
- 尝试从我的节点服务器加载脚本文件 - 获取 404
- 需要从外部文件获取JSON