Javascript 使用ajax发送输入文件数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27936645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send input files data with ajax
提问by Hassan Ali
html markup:
html标记:
<input id="fileSelect" type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
I am uploading multiples files with php. I want to make an array of upload files and send to server with ajax. how to make an array of the multiple selected files?
我正在用 php 上传多个文件。我想制作一系列上传文件并使用ajax发送到服务器。如何制作多个选定文件的数组?
JavaScript:
JavaScript:
jQuery.ajax({
url: 'insertfiles.php',
type: "POST",
data: {
file: // array of selected files.
},
success: function(data){
},
error: function(data){
alert( 'Sorry.' );
}
});
采纳答案by ianaya89
Modern browsers that support the HTML5file stuff have in the <input>element a "files" property.
That will give you a file list reference, which has a lengthproperty.
支持HTML5文件内容的现代浏览器在<input>元素中有一个“文件”属性。这会给你一个文件列表引用,它有一个length属性。
As the property is already an arrayso you just need to access it or iterate through it.
由于该属性已经是一个,array所以您只需要访问它或遍历它。
JS
JS
var input = document.getElementById('id');
console.log(input.files);
for (var i = 0; i < input.files.length; i++) {
console.log(input.files[i]);
}
回答by Utkarsh Dixit
Use the code below.
使用下面的代码。
var formData = new FormData($("#formid")[0]);
jQuery.ajax
({
url: 'insertfiles.php',
type: "POST",
data: formData;
success: function(data)
{
},
error: function(data)
{
alert( 'Sorry.' );
}
cache: false,
contentType: false,
processData: false
});
Hope this helps you
希望这对你有帮助
回答by TechGirl
var formData = new FormData(this);
debugger;
$.ajax({
url: formURL,
type: 'POST',
data: formData,
mimeType: "multipart/form-data",
contentType: false,
cache: false,
processData: false,
success: function (data, textStatus, jqXHR) {
debugger;
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
The above code will help you post content plus files in one submit call.
上面的代码将帮助您在一次提交调用中发布内容和文件。
The post method parameter should include HttpPostedFileBase[] fileso the list of files will appear in this file parameter
post方法参数应该包括HttpPostedFileBase[] file这样文件列表将出现在这个文件参数中
回答by Shivam
This is my code for multiple file upload. Please refer this
这是我的多文件上传代码。请参考这个
$fileCount = count($_FILES);
for($i=0; $i < $fileCount; $i++) {
$fileName = (!empty($_FILES["file-$i"]["name"])) ? $_FILES["file-$i"]["name"] : '';
$hostName = "REDACTED";
$userName = "REDACTED";
$passWord = "REDACTED";
$dbName = "b7_15386696_db_printer";
$query = "INSERT INTO print_table (NAME, EMAIL, PHONE, ADDRESS, FILENAME, NUMBEROFCOPY, SREQUIREMENTS, FIELD1)
VALUES ('$name', '$emailId', '$phone', '$address', '$fileName', '$ncopy', '$requirements', CONVERT_TZ(NOW(),'+00:00','+09:30'))";
$connect = mysqli_connect($hostName,"$userName","$passWord","$dbName");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

