我有一个页面文件 file-upload.jsp,其中包含以下代码片段:
<form action="" id="frmupload" name="frmupload" method="post" enctype="multipart/form-data">
<input type="file" id="upload_file" name="upload_file" multiple="" />
<input type="submit" value="Update" />
</form>
我有两个问题:
The moment I select some files, i.e the
onchange
event of theinput
typefile
, the file(s) should get uploaded.I have a Java page that receives multipart request parameter and uploads the file to the said location. My problem is the form submission
onchange
, so that the Java file can proceed with further operations.I googled and went through lot of articles. Some say it's not possible to upload files directly via Ajax, some say submit the form to an iframe via Ajax/jQuery.
I tried a lot of code from internet, such as this:
$(document).ready(function(){ $('upload_file').change(function(){ var data = new FormData(); data.append('file', $(this[0].files[0])); $.ajax({ url: 'photo.jsp', type: 'post', contentType: attr('enctype', "multipart/form-data"), data: data, success: function(data){ alert(data); } }) }); });
but could not get the expected results.
I also need a progress bar for the upload operation.