Review
In the previous section, we have written the configuration files. In this section, we will write the HTML form for uploading files.Table of Contents
Part 1: Introduction and Functional SpecsPart 2: Java classes
Part 3: XML configuration
Part 4: HTML form
Part 5: Running the Application
To achieve a seamless AJAX-like file upload experience, we will be using blueimp's jQuery-File-Upload plugin for jQuery.
What is jQuery-File-Upload?
File Upload widget with multiple file selection, drag&drop support, progress bars and preview images for jQuery. Supports cross-domain, chunked and resumable file uploads and client-side image resizing. Works with any server-side platform (PHP, Python, Ruby on Rails, Java, Node.js, Go etc.) that supports standard HTML form file uploads.
Source: https://github.com/blueimp/jQuery-File-Upload
File Upload widget with multiple file selection, drag&drop support, progress bars and preview images for jQuery. Supports cross-domain, chunked and resumable file uploads and client-side image resizing. Works with any server-side platform (PHP, Python, Ruby on Rails, Java, Node.js, Go etc.) that supports standard HTML form file uploads.
Source: https://github.com/blueimp/jQuery-File-Upload
To test-drive this plugin, please visit the following demos:
- Demo File Upload
- How to setup the plugin on your website
- How to use only the basic plugin (minimal setup guide).
For our purposes, we will follow the minimal setup guide, so that we can create our custom UI and eliminate extraneous steps.
Warning!
Make sure to test the plugin on different browsers. Not all browsers behave the same way. In my opinion, Chrome and Firefox are the best for development.
Html Form
Here's our upload form again:Here's the full source:
Let's dissect this source and make sense of it.
Endpoint Urls
We've declared two global urls: messageUploadUrl and fileUploadUrl. These are the endpoints for uploading messages and files respectively.
Import Scripts
Based on the jQuery-File-Upload basic plugin guide, we need to import the following scripts: jquery.ui.widget.js, jquery.iframe-transport.js, and jquery.fileupload.js. The util.js contains a method for posting JSON objects. The remaining scripts are for the jQuery framework itself.
Initialization Function
This function contains the following initialization steps:
- Beautify buttons
- Attach submit function
- Attach clear function
- Attach file upload function
- Initialize filelist data
File Display Function
This is used for formatting filenames. The output is similar to the following: filename (256.50K)
File List Function
This is used to retrieved the list of filenames. The output is comma-delimited, which is similar to the following: filename1.jpg,filename2.doc,filename3.txt
Dialog Function
This is a helper function for displaying dialog boxes.
Clear Function
This clears the form of its contents.
Form
This is the form itself. Notice we've set the file input's opacity to zero to hide it and make it work under Safari and Opera (try removing the opacity and hide the file input via display:none to see an unwanted effect).
By setting the file input's opacity to 0, we have successfully hidden the input. But how do we access it now? Notice the "Add a file" link? We've attached a click trigger function on that link to fire up the file input. If we removed the opacity, here's what we will see:
Chrome 19.0.1084.56
Firefox 9.0.1
Internet Explorer 9.0.8112.16421
Browser Bugs
Unfortunately, even though the jQuery-File-Upload plugin has great cross-browser support, HTML5 and traditional, there are still bugs and unwanted behavior. Maybe it's a configuration issue or a browser issue. In other words, always tests before you deploy to production. Here's what I've discovered so far:Internet Explorer 9.0.8112.16421
- Does not upload file
- Does not upload message
Opera 11.60
- Attach link does not work if file input has display:none
- To make it work, set the opacity instead
Safari 5.1.2
- Attach link does not work if file input has display:none
- To make it work, set the opacity instead
Next
In the next section, we will build and run the application using Maven, and show how to import the project in Eclipse. Click here to proceed.
Share the joy:
|
Subscribe by reader Subscribe by email Share
Give me the download link to this code...
ReplyDeleteif u have other examples for multiple files uploading please give it...
ReplyDeleteHi Krams,
ReplyDeleteThanks for the nice tutorial. If I have to pass files instead of filename then what I have to do modify in your jquery code
Krams! Thank you very much for the practical demonstration! I would never have been able to implement the file upload library in my project without it!
ReplyDeleteWith this set I get "this._on is not a function" from jquery.fileupload.js even though jquery.widget.ui.js is included before it.
ReplyDeleteDid you get this error when running the sample project or from your own custom project? What browser are you using? Make sure to try another browser
DeleteMade a custom project and tried with FF12.0 and Chromium.
DeleteI started from scratch and now I get "this.hoverable is not a function" from jquery.ui.widget.js. I have included javascripts in my liferay project with tag
This could be a compatibility issue with that widget or a conflict with your existing scripts. Have you checked the compatibility page at https://github.com/blueimp/jQuery-File-Upload?
DeleteMaybe the following link will provide some light? http://stackoverflow.com/questions/10633607/jquery-ui-widget-is-not-a-function-with-autocomplete-combobox
Thanks. How you can fix this work on Internet Explorer 9?
ReplyDeleteI don't have IE9. And what specifically needs fixing? I'm running Linux.
DeleteHi Thanks for all important tutorial. I want to implement one to many relationship of your hibernate spring view in jqgrid. but creditcards column is showing object.object. please share one jsp to parse one to many relationship in jqgrid. I am waiting for your response.
ReplyDeleteThanks
dileep maurya India
Thanks krams. it'll be very help full if you validate the uploaded file with spring validator interface.
ReplyDeleteHi Kram,
ReplyDelete.config() is not getting called from app.js angular.module('myAppNew',['blueimp.fileupload']); actually while debugging i found that this.$get of jquery.fileupload-angular.js is not getting called which returns defaults: $config. let me know whats could be the possible solution for this.
Mark - when selecting files to add, I get my "Adding files, please wait" message to display, but once all the files are displayed - the message won't go away. Any ideas why this would happen?
ReplyDeleteDoes any one has a solution for Internet Explorer. It is working fine for Chrome, but I need it for IE.
ReplyDeleteI have read your blog its very attractive and impressive. I like it your blog.
ReplyDeleteSpring online training Spring online training Spring Hibernate online training Spring Hibernate online training Java online training
spring training in chennai spring hibernate training in chennai
After reading this blog i very strong in this topics and this blog really helpful to all Ruby on Rails Online Course
ReplyDeleteGood Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeletecore java training in Electronic City
Hibernate Training in electronic city
spring training in electronic city
java j2ee training in electronic city
I enjoyed your blog Thanks for sharing such an informative post. We are also providing the best services click on below links to visit our website.
ReplyDeletedigital marketing company in nagercoil
digital marketing services in nagercoil
digital marketing agency in nagercoil
best marketing services in nagercoil
SEO company in nagercoil
SEO services in nagercoil
social media marketing in nagercoil
social media company in nagercoil
PPC services in nagercoil
digital marketing company in velachery
digital marketing company in velachery
digital marketing services in velachery
digital marketing agency in velachery
SEO company in velachery
SEO services in velachery
social media marketing in velachery
social media company in velachery
PPC services in velachery
online advertisement services in velachery
online advertisement services in nagercoil
web design company in nagercoil
web development company in nagercoil
website design company in nagercoil
website development company in nagercoil
web designing company in nagercoil
website designing company in nagercoil
best web design company in nagercoil
web design company in velachery
web development company in velachery
website design company in velachery
website development company in velachery
web designing company in velachery
website designing company in velachery
best web design company in velachery
Thanks for Sharing - ( Groarz branding solutions )