Introduction
In this article, we will study how to do file uploads with Spring and jQuery. We will learn how to attach multiple files and use the jQuery-File-Upload plugin to provide a seamless file upload experience.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
Dependencies
- Spring core 3.1.0.RELEASE
- jQuery-File-Upload
- See pom.xml for details
Github
To access the source code, please visit the project's Github repository (click here)
Functional Specs
Let's define our application's requirements:- Create a simple form where users can upload multiple files
- Users should be able to add an owner name and description for each upload
- Provide an AJAX-like experience
Here's our Use Case diagram:
File Upload Strategy
To achieve an AJAX-like experience we have to resort to a different strategy when uploading and sending form data. Instead of sending all form data in one go, we will upload the file separately (behind the scenes without user intervention).
Here are the steps:
- User fills-in the form's text inputs, i.e owner and description
- User clicks on "Add a file" link. Browsers for a file and attaches it
- Behind the scene, the form uploads the file to the server. The server saves the file and returns the file details, i.e filename and file size
- User clicks on "Upload" button. The form does a POST action to send the form data. But it never sends the file itself. Instead it sends the filename that was saved by the server. The user is tricked into thinking that the file hasn't been uploaded yet.
Screenshots
Let's preview how our application will look like after it has been completed. This is also a good way to clarify further our application's specsThis is the entry page where users can upload files. Users are allowed to attach multiple files.
When attaching multiple files, this is the expected output.
This alert shows whenever files have been successfully uploaded.
This alerts shows whenever files have failed to be uploaded.
Next
In the next section, we will start writing the Java classes. Click here to proceed.
Share the joy:
|
Subscribe by reader Subscribe by email Share
Hi Krams,
ReplyDeleteI am getting this error on page load.... (check from firebug console).
Type Error: this.hoverable is undefined.
this.hoverable.removeClass(...state-hover");
-- Jquery...widget.js (line 344)
Please mail me @ krishna@rupeelog.com
This comment has been removed by the author.
ReplyDeletethanks! very cool!
ReplyDeleteHi. Thanks for share, your post help me so much
ReplyDeleteBut, I have a question, what if I have a form which contains input type file and text?
I want to upload single file and text description, and catch it into some Model, such as FileMeta{ MutipartFile file, String description }
I try to build my own.. but I always get this exception: org.springframework.web.multipart.MultipartException: The current request is not a multipart request
I've been thinking and searching for solution for two days, but still not found it. Can u help me solve this?
it won't give the full path of file
ReplyDeleteHi, can you please give us the logic to show progress bars for individual files and also consolidated progress bar. It will be much helpful. Thanks.
ReplyDeleteThank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
ReplyDeleteAWS Training in Bangalore
AWS training in sholinganallur
AWS training in Tambaram
AWS training in Velachery
Nice Post.
ReplyDeleteFor Data Science training in Bangalore, Visit:
Data Science training in Bangalore
Internet Business Ideas for 2020
ReplyDeleteThis is a great post thanks
ReplyDelete