Html5 implements asynchronous file upload,

Source: Internet
Author: User

Html5 implements asynchronous file upload,

1 Overview

Developing the file upload function is never a pleasant task. asynchronous upload is even more so. I have used the iframe and Flash upload schemes, and I feel awkward. This article briefly introduces how to use FormData of Html5 to Implement Asynchronous file upload, as well as upload progress bars and file size verification. The server uses the springMVC solution for processing.

2. Html code

<Form id = "myForm"> <input type = "file" id = "u_photo" name = "u_photo"/> <input type = "button" id = "submit-btn "value =" Upload "/> </form>

3 JQuery upload

$ ("# Submit-btn "). on ('click', function () {$. ajax ({url: "/test/upload", type: "post", data: new FormData ($ ("# myForm "). get (0), // is very important. cache: false, processData: false, contentType: false, success: function () {alert ("uploaded successfully! ");}});});

4. JQuery file size verification

The file size and corresponding behavior control should be handled as needed. This method is just an example.

$ ('# U_photo '). on ('change', function () {var file = this. files [0]; if (file. size> 1024*1000) {alert ('the maximum file size is 1 MB! ')}});

5 JQuery progress bar

You can add xhr to the ajax method to control the upload progress. You can use the html5 progress bar or other progress bars. Display and hide progress bars must be handled by yourself. This method only briefly introduces the basic control of progress bars.

xhr: function() {    var myXhr = $.ajaxSettings.xhr();    if (myXhr.upload) {        myXhr.upload.addEventListener('progress', function(e) {            if (e.lengthComputable) {                $('progress').attr({                    value: e.loaded,                    max: e.total,                });            }        } , false);    }    return myXhr;}

6 springMVC Server

6.1 maven dependency

<dependency>    <groupId>commons-io</groupId>    <artifactId>commons-io</artifactId>    <version>2.5</version></dependency><dependency>    <groupId>commons-fileupload</groupId>    <artifactId>commons-fileupload</artifactId>    <version>1.3.2</version></dependency>

6.2 servlet-context.xml

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

6.3 Controller

The sample program does not provide code for file verification, storage, and processing.

@RequestMapping(value="/test/upload",method = RequestMethod.POST)@ResponseBodypublic String upload(@RequestParam("u_photo") MultipartFile u_photo) {    System.out.println("u_photo="+u_photo.getSize());    return "ok";}

7 compatibility

IE 10 +, Firefox 4.0 +, Chrome 7 +, Safari 5 +, and Opera 12 +

8. Recommended reading

If you are not satisfied with the above solution, the following solution is recommended:

JQuery File Uploader

The above is the Html5 file asynchronous upload function introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.