Reputation: 117
I want to do multiple file upload. I tried lot but I'm not able to get the solution. Whenever I am uploading multiple images, it is taking only first image value.
formData.append('file[]', $('input[type=file]')[0].files[0]);
I am trying like this I am getting single image
formData.append('file[]', $('input[type=file]')[1].files[1]);
I am trying like this I am getting error
Uncaught TypeError: Cannot read property 'files' of undefined
$("#basicFormBtn").click(function(e){
e.preventDefault();
var formData = new FormData();
var formData = new FormData($('#basicForm')[0]);
formData.append('file[]', $('input[type=file]')[0].files[0]);
$.ajax({
type:'POST',
url :"test_session.php",
data: formData,
cache: false,
contentType: false,
processData: false,
success: function(data) {
console.log(data);
},
error:function(exception){
alert('Exeption:'+exception);
}
});
});
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Basic Form</h2>
<form id="basicForm">
<div class="row">
<div class="col-md-5 form-group">
<label for="name">User Name:</label>
<input type="text" class="form-control" id="userName" placeholder="Enter userName" name="userName" required="">
</div>
</div>
<div class="row">
<div class="col-md-5 form-group">
<label for="album">Album:</label>
<input type="file" name="file" required="" multiple>
</div>
</div>
<input type="submit" name="submit" value="submit" class="btn btn-info" id="basicFormBtn">
</form>
</div>
</body>
</html>
I'm getting output like this (in test_session.php
) with print_r($_FILES)
;
Array
(
[file] => Array
(
[name] => Array
(
[0] => 1.png
)
[type] => Array
(
[0] => image/png
)
[tmp_name] => Array
(
[0] => C:\xampp5.6\tmp\phpC3C0.tmp
)
[error] => Array
(
[0] => 0
)
[size] => Array
(
[0] => 166275
)
)
)
Upvotes: 0
Views: 5241
Reputation: 947
that's because you not set array value to your append file input. try this :
var i=0;//set default array value
$("#basicFormBtn").click(function(e){
i++;//set next array value
e.preventDefault();
var formData = new FormData();
var formData = new FormData($('#basicForm')[0]);
formData.append('file['+i+']', $('input[type=file]')[0].files[0]);//push next array value
$.ajax({
type:'POST',
url :"test_session.php",
data: formData,
cache: false,
contentType: false,
processData: false,
success: function(data) {
console.log(data);
},
error:function(exception){
alert('Exeption:'+exception);
}
});
});
Upvotes: 0
Reputation: 347
Try this you will get all uploaded files.
Your old code
<input type="file" name="file" required="" multiple>
Change with this
<input type="file" name="file[]" required="" multiple>
Upvotes: 2