Reputation: 99
I have this code
<input id="fileupload"
type="file" name="files[]"
class="inputFile clickable"
onchange="uploadFile(this.value)"/>
This works fine on second time using it, i.e. first time I select the file the onchange event does not fire, but selecting file for the second time works fine.
Is there any thing which I can change here?
I have tried:
onlclick
(doesn't work, fires before selecting file)onblur
(doesn't work, doesn't fire at all, plus even if does, its just stupid to click somewhere else on page to fire the operation)onselect
(doesn't work)Additional info:
If I use onchange=alert(this.value)
it works fine
this is my javascript code
function uploadFile(value) {
alert(value); //works fine
$('#fileupload').fileupload({
dataType: 'json',
url: 'fileUpload.php',
type: 'POST',
limitConcurrentUploads: 1,
// done: function (e, data) {
// $.each(data.result, function (index, file) {
// $('<p/>').text(file.name).appendTo(document.body);
// });
// },
success: function() {
showMultipleDataDiv(value); //but I don't get value here
}
});
}
Upvotes: 2
Views: 12049
Reputation: 5535
The code $('#fileupload').fileupload
will trigger a file upload as soon as the user selects a file. You need to run this code before the user selects a file, because it adds an event listener to the input tag. Since you only run this code after the user selects a file, then it will only work on the second time.
This is the change you need to do to make it work
$(function () {
$('#fileupload').fileupload({
dataType: 'json',
url: 'fileUpload.php',
type: 'POST',
limitConcurrentUploads: 1,
// done: function (e, data) {
// $.each(data.result, function (index, file) {
// $('<p/>').text(file.name).appendTo(document.body);
// });
// },
success: function () {
showMultipleDataDiv(value); //but I don't get value here
}
});
});
Notice there is no need to add code in the change event. The plugin is going to do that for you.
You can read more about it here: https://github.com/blueimp/jQuery-File-Upload/wiki/Basic-plugin
Upvotes: 1