Upload operation of file asynchronously using jquery
Unlike you think, the code does NOT use that plugin to upload files. Instead you explicitly make an ajax request. The error occurs because the value of the <input type="file">
is the filename, and that is the only data you are sending in the request.
Instead you need to bind the form using $(form).ajaxform()
; then in the click handler, you can trigger the submit event on the form.
Thus something like as follows ought to do the trick:
html:
<form method="post" action="addFile.do">
<input type="file" id="f" name="f"/>
<input id="upload" type="button" value="Upload"/>
</form>
And JavaScript:
$('form').ajaxform({
success: function () {
alert("All Files Have Been Uploaded ");
}
});
$("#upload").click(function() {
$('form').submit();
});
How to make JQuery file upload plugin call backend only once for all the files in an upload?
Setting SingleFileUploads
to false didn't help much with JQuery file uploader since there seems to be a bug as discussed above. So I set that thing back to true.
I separated the inputs into two separate forms - one for text fields input and one for files(which goes through JQuery file uploader). For the text fields form, I kept a visible button the user can click. For the other one, I hid the button. So once the user clicks the visible button, I submit only the text input and create a database record in the backend(this is done using an AJAX call) and in the success
field of AJAX call, I .click()
the hidden button if the file count is more than 0.
Related Topics
Is Chrome'S JavaScript Console Lazy About Evaluating Objects
"Variable" Variables in JavaScript
How to Return the Response from an Observable/Http/Async Call in Angular
How to Correctly Clone a JavaScript Object
Is JavaScript a Pass-By-Reference or Pass-By-Value Language
JavaScript Property Access: Dot Notation Vs. Brackets
How to Merge Properties of Two JavaScript Objects Dynamically
Difference Between Json and Object Literal Notation
How to Remove a Specific Item from an Array
Is There a Regexp.Escape Function in JavaScript
Object Comparison in JavaScript
How to Convert Base64 String to JavaScript File Object Like as from File Input Form
What Do Curly Braces in JavaScript Mean
How to Detect a Mobile Device With JavaScript
Modifying CSS Class Property Values on the Fly With JavaScript/Jquery