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?










share|improve this question
























  • 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















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?










share|improve this question
























  • 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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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]);
}





share|improve this answer





















  • 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










  • Brilliant, thank you. I got it :D
    – Adrian
    Nov 8 at 14:33











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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]);
}





share|improve this answer





















  • 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










  • Brilliant, thank you. I got it :D
    – Adrian
    Nov 8 at 14:33















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]);
}





share|improve this answer





















  • 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










  • Brilliant, thank you. I got it :D
    – Adrian
    Nov 8 at 14:33













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]);
}





share|improve this answer












event.target.files is a array, so you need the first file in the array



function prepareUpload(event){
files.push(event.target.files[0]);
}






share|improve this answer












share|improve this answer



share|improve this answer










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 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










  • 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










  • 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
















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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Schultheiß

Liste der Kulturdenkmale in Wilsdruff

Android Play Services Check