Uploading multiple files from different inputs using Ajax
up vote
0
down vote
favorite
I have two different file inputs and multiple textual/select inputs, that I'd like to upload to a PHP file using Ajax. The file inputs are meant to send images, and because they are two specific images that I'd like to identify by the name of the input, I do not want to use <input type="file" multiple>
.
Currently I have the following (names have been changed to keep it simple):
<input type="file" name="file1">
<input type="file" name="file2">
<textarea name="text1"></textarea>
<button>Submit</button>
What I have tried is to push both to a variable once the change event of the file input gets fired, followed by the button press triggering the upload using Ajax.
$('input[type="file"][name="file1"]').on('change', prepareUpload);
$('input[type="file"][name="file2"]').on('change', prepareUpload);
files = new Array;
function prepareUpload(event){
files.push(event.target.files);
}
$('button').on('click', uploadFiles);
function uploadFiles(event){
$('#uploadInfo').html('Uploading...');
event.stopPropagation();
event.preventDefault();
var data = new FormData();
$.each(files, function(key, value){
data.append(key, value);
});
data.append('text1', $('textarea[name="text1"]').val());
$.ajax({
url: '',
type: 'POST',
data: data,
cache: false,
dataType: 'json',
processData: false,
contentType: false,
success: function(result){
//do stuff
},
error: function(error){
console.log(error);
}
});
}
But this seems to do nothing. Is it even possible to append two image inputs to the same request using Ajax, or am I better off trying to put it in two different requests?
javascript php jquery ajax file-upload
|
show 1 more comment
up vote
0
down vote
favorite
I have two different file inputs and multiple textual/select inputs, that I'd like to upload to a PHP file using Ajax. The file inputs are meant to send images, and because they are two specific images that I'd like to identify by the name of the input, I do not want to use <input type="file" multiple>
.
Currently I have the following (names have been changed to keep it simple):
<input type="file" name="file1">
<input type="file" name="file2">
<textarea name="text1"></textarea>
<button>Submit</button>
What I have tried is to push both to a variable once the change event of the file input gets fired, followed by the button press triggering the upload using Ajax.
$('input[type="file"][name="file1"]').on('change', prepareUpload);
$('input[type="file"][name="file2"]').on('change', prepareUpload);
files = new Array;
function prepareUpload(event){
files.push(event.target.files);
}
$('button').on('click', uploadFiles);
function uploadFiles(event){
$('#uploadInfo').html('Uploading...');
event.stopPropagation();
event.preventDefault();
var data = new FormData();
$.each(files, function(key, value){
data.append(key, value);
});
data.append('text1', $('textarea[name="text1"]').val());
$.ajax({
url: '',
type: 'POST',
data: data,
cache: false,
dataType: 'json',
processData: false,
contentType: false,
success: function(result){
//do stuff
},
error: function(error){
console.log(error);
}
});
}
But this seems to do nothing. Is it even possible to append two image inputs to the same request using Ajax, or am I better off trying to put it in two different requests?
javascript php jquery ajax file-upload
Hi and Welcome to Stack Overflow. This is not a solution to your problem but a comment: You have two files with the same name in your HTML. This will not work. I believe it is just a typo, but it may be in your code also.
– cmprogram
Nov 8 at 13:18
Ah thanks, it's not in my HTML, as I changed all names to make it a bit more simple for other possible use cases and others maybe having the same question :)
– Adrian
Nov 8 at 13:21
any console errors?
– madalinivascu
Nov 8 at 13:27
None, ajax only fires the error function as it isn't getting json back from my PHP file. But the PHP file is getting an empty request.
– Adrian
Nov 8 at 14:16
how do you know its getting an empty request?
– madalinivascu
Nov 8 at 14:20
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have two different file inputs and multiple textual/select inputs, that I'd like to upload to a PHP file using Ajax. The file inputs are meant to send images, and because they are two specific images that I'd like to identify by the name of the input, I do not want to use <input type="file" multiple>
.
Currently I have the following (names have been changed to keep it simple):
<input type="file" name="file1">
<input type="file" name="file2">
<textarea name="text1"></textarea>
<button>Submit</button>
What I have tried is to push both to a variable once the change event of the file input gets fired, followed by the button press triggering the upload using Ajax.
$('input[type="file"][name="file1"]').on('change', prepareUpload);
$('input[type="file"][name="file2"]').on('change', prepareUpload);
files = new Array;
function prepareUpload(event){
files.push(event.target.files);
}
$('button').on('click', uploadFiles);
function uploadFiles(event){
$('#uploadInfo').html('Uploading...');
event.stopPropagation();
event.preventDefault();
var data = new FormData();
$.each(files, function(key, value){
data.append(key, value);
});
data.append('text1', $('textarea[name="text1"]').val());
$.ajax({
url: '',
type: 'POST',
data: data,
cache: false,
dataType: 'json',
processData: false,
contentType: false,
success: function(result){
//do stuff
},
error: function(error){
console.log(error);
}
});
}
But this seems to do nothing. Is it even possible to append two image inputs to the same request using Ajax, or am I better off trying to put it in two different requests?
javascript php jquery ajax file-upload
I have two different file inputs and multiple textual/select inputs, that I'd like to upload to a PHP file using Ajax. The file inputs are meant to send images, and because they are two specific images that I'd like to identify by the name of the input, I do not want to use <input type="file" multiple>
.
Currently I have the following (names have been changed to keep it simple):
<input type="file" name="file1">
<input type="file" name="file2">
<textarea name="text1"></textarea>
<button>Submit</button>
What I have tried is to push both to a variable once the change event of the file input gets fired, followed by the button press triggering the upload using Ajax.
$('input[type="file"][name="file1"]').on('change', prepareUpload);
$('input[type="file"][name="file2"]').on('change', prepareUpload);
files = new Array;
function prepareUpload(event){
files.push(event.target.files);
}
$('button').on('click', uploadFiles);
function uploadFiles(event){
$('#uploadInfo').html('Uploading...');
event.stopPropagation();
event.preventDefault();
var data = new FormData();
$.each(files, function(key, value){
data.append(key, value);
});
data.append('text1', $('textarea[name="text1"]').val());
$.ajax({
url: '',
type: 'POST',
data: data,
cache: false,
dataType: 'json',
processData: false,
contentType: false,
success: function(result){
//do stuff
},
error: function(error){
console.log(error);
}
});
}
But this seems to do nothing. Is it even possible to append two image inputs to the same request using Ajax, or am I better off trying to put it in two different requests?
javascript php jquery ajax file-upload
javascript php jquery ajax file-upload
edited Nov 8 at 13:27
asked Nov 8 at 13:14
Adrian
33
33
Hi and Welcome to Stack Overflow. This is not a solution to your problem but a comment: You have two files with the same name in your HTML. This will not work. I believe it is just a typo, but it may be in your code also.
– cmprogram
Nov 8 at 13:18
Ah thanks, it's not in my HTML, as I changed all names to make it a bit more simple for other possible use cases and others maybe having the same question :)
– Adrian
Nov 8 at 13:21
any console errors?
– madalinivascu
Nov 8 at 13:27
None, ajax only fires the error function as it isn't getting json back from my PHP file. But the PHP file is getting an empty request.
– Adrian
Nov 8 at 14:16
how do you know its getting an empty request?
– madalinivascu
Nov 8 at 14:20
|
show 1 more comment
Hi and Welcome to Stack Overflow. This is not a solution to your problem but a comment: You have two files with the same name in your HTML. This will not work. I believe it is just a typo, but it may be in your code also.
– cmprogram
Nov 8 at 13:18
Ah thanks, it's not in my HTML, as I changed all names to make it a bit more simple for other possible use cases and others maybe having the same question :)
– Adrian
Nov 8 at 13:21
any console errors?
– madalinivascu
Nov 8 at 13:27
None, ajax only fires the error function as it isn't getting json back from my PHP file. But the PHP file is getting an empty request.
– Adrian
Nov 8 at 14:16
how do you know its getting an empty request?
– madalinivascu
Nov 8 at 14:20
Hi and Welcome to Stack Overflow. This is not a solution to your problem but a comment: You have two files with the same name in your HTML. This will not work. I believe it is just a typo, but it may be in your code also.
– cmprogram
Nov 8 at 13:18
Hi and Welcome to Stack Overflow. This is not a solution to your problem but a comment: You have two files with the same name in your HTML. This will not work. I believe it is just a typo, but it may be in your code also.
– cmprogram
Nov 8 at 13:18
Ah thanks, it's not in my HTML, as I changed all names to make it a bit more simple for other possible use cases and others maybe having the same question :)
– Adrian
Nov 8 at 13:21
Ah thanks, it's not in my HTML, as I changed all names to make it a bit more simple for other possible use cases and others maybe having the same question :)
– Adrian
Nov 8 at 13:21
any console errors?
– madalinivascu
Nov 8 at 13:27
any console errors?
– madalinivascu
Nov 8 at 13:27
None, ajax only fires the error function as it isn't getting json back from my PHP file. But the PHP file is getting an empty request.
– Adrian
Nov 8 at 14:16
None, ajax only fires the error function as it isn't getting json back from my PHP file. But the PHP file is getting an empty request.
– Adrian
Nov 8 at 14:16
how do you know its getting an empty request?
– madalinivascu
Nov 8 at 14:20
how do you know its getting an empty request?
– madalinivascu
Nov 8 at 14:20
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
event.target.files
is a array, so you need the first file in the array
function prepareUpload(event){
files.push(event.target.files[0]);
}
Thanks, this made it a little more clear for me. Do the new files get prepended or appended to the array? (is it fair to continuously useevent.target.files[0]
for every file or do I need to increment the 0?)
– Adrian
Nov 8 at 14:24
you have multiple inputs with one file each so you will always haveevent.target.files[0]
– madalinivascu
Nov 8 at 14:29
Brilliant, thank you. I got it :D
– Adrian
Nov 8 at 14:33
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
event.target.files
is a array, so you need the first file in the array
function prepareUpload(event){
files.push(event.target.files[0]);
}
Thanks, this made it a little more clear for me. Do the new files get prepended or appended to the array? (is it fair to continuously useevent.target.files[0]
for every file or do I need to increment the 0?)
– Adrian
Nov 8 at 14:24
you have multiple inputs with one file each so you will always haveevent.target.files[0]
– madalinivascu
Nov 8 at 14:29
Brilliant, thank you. I got it :D
– Adrian
Nov 8 at 14:33
add a comment |
up vote
1
down vote
accepted
event.target.files
is a array, so you need the first file in the array
function prepareUpload(event){
files.push(event.target.files[0]);
}
Thanks, this made it a little more clear for me. Do the new files get prepended or appended to the array? (is it fair to continuously useevent.target.files[0]
for every file or do I need to increment the 0?)
– Adrian
Nov 8 at 14:24
you have multiple inputs with one file each so you will always haveevent.target.files[0]
– madalinivascu
Nov 8 at 14:29
Brilliant, thank you. I got it :D
– Adrian
Nov 8 at 14:33
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
event.target.files
is a array, so you need the first file in the array
function prepareUpload(event){
files.push(event.target.files[0]);
}
event.target.files
is a array, so you need the first file in the array
function prepareUpload(event){
files.push(event.target.files[0]);
}
answered Nov 8 at 13:30
madalinivascu
26.6k21740
26.6k21740
Thanks, this made it a little more clear for me. Do the new files get prepended or appended to the array? (is it fair to continuously useevent.target.files[0]
for every file or do I need to increment the 0?)
– Adrian
Nov 8 at 14:24
you have multiple inputs with one file each so you will always haveevent.target.files[0]
– madalinivascu
Nov 8 at 14:29
Brilliant, thank you. I got it :D
– Adrian
Nov 8 at 14:33
add a comment |
Thanks, this made it a little more clear for me. Do the new files get prepended or appended to the array? (is it fair to continuously useevent.target.files[0]
for every file or do I need to increment the 0?)
– Adrian
Nov 8 at 14:24
you have multiple inputs with one file each so you will always haveevent.target.files[0]
– madalinivascu
Nov 8 at 14:29
Brilliant, thank you. I got it :D
– Adrian
Nov 8 at 14:33
Thanks, this made it a little more clear for me. Do the new files get prepended or appended to the array? (is it fair to continuously use
event.target.files[0]
for every file or do I need to increment the 0?)– Adrian
Nov 8 at 14:24
Thanks, this made it a little more clear for me. Do the new files get prepended or appended to the array? (is it fair to continuously use
event.target.files[0]
for every file or do I need to increment the 0?)– Adrian
Nov 8 at 14:24
you have multiple inputs with one file each so you will always have
event.target.files[0]
– madalinivascu
Nov 8 at 14:29
you have multiple inputs with one file each so you will always have
event.target.files[0]
– madalinivascu
Nov 8 at 14:29
Brilliant, thank you. I got it :D
– Adrian
Nov 8 at 14:33
Brilliant, thank you. I got it :D
– Adrian
Nov 8 at 14:33
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208514%2fuploading-multiple-files-from-different-inputs-using-ajax%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hi and Welcome to Stack Overflow. This is not a solution to your problem but a comment: You have two files with the same name in your HTML. This will not work. I believe it is just a typo, but it may be in your code also.
– cmprogram
Nov 8 at 13:18
Ah thanks, it's not in my HTML, as I changed all names to make it a bit more simple for other possible use cases and others maybe having the same question :)
– Adrian
Nov 8 at 13:21
any console errors?
– madalinivascu
Nov 8 at 13:27
None, ajax only fires the error function as it isn't getting json back from my PHP file. But the PHP file is getting an empty request.
– Adrian
Nov 8 at 14:16
how do you know its getting an empty request?
– madalinivascu
Nov 8 at 14:20