This article briefly introduces the use of Html5's FormData to implement asynchronous uploading of files, and can also implement upload progress bars and file size verification. The code is simple and easy to understand, very good, and has reference value. Friends who need it can refer to it
1 Introduction
Developing a file upload function is never a pleasant thing, especially asynchronous upload. I have used iframe and Flash upload solutions. , also felt very awkward. This article briefly introduces the use of Html5's FormData to implement asynchronous uploading of files, and can also implement 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="上传" /> </form>
3 JQuery upload
$("#submit-btn").on('click', function() { $.ajax({ url:"/test/upload", type:"post", data:new FormData($("#myForm").get(0)), //十分重要,不能省略 cache: false, processData: false, contentType: false, success: function () { alert("上传成功!"); } }); });
4 JQuery file size verification
File size and corresponding behavior control, You need to handle it yourself as needed. This method is just an example method.
$('#u_photo').on('change', function() { var file = this.files[0]; if (file.size > 1024*1000) { alert('文件最大1M!') } });
5 JQuery progress bar
Add xhr to the ajax method to control the upload progress. The progress bar can use HTML5's progress or other progress bars. Displaying and hiding the progress bar needs to be handled by yourself. This method only briefly introduces the basic control of the progress bar.
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 the corresponding code for file verification, storage and processing.
@RequestMapping(value="/test/upload",method = RequestMethod.POST) @ResponseBody public 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+, Opera 12+
8 Recommended reading
If you are not satisfied with the above solution, we recommend the following solution:
JQuery File Uploader
The above is the detailed content of Implementation of Html5 file asynchronous upload function. For more information, please follow other related articles on the PHP Chinese website!

A reasonable H5 code structure allows the page to stand out among a lot of content. 1) Use semantic labels such as, etc. to organize content to make the structure clear. 2) Control the rendering effect of pages on different devices through CSS layout such as Flexbox or Grid. 3) Implement responsive design to ensure that the page adapts to different screen sizes.

The main differences between HTML5 (H5) and older versions of HTML include: 1) H5 introduces semantic tags, 2) supports multimedia content, and 3) provides offline storage functions. H5 enhances the functionality and expressiveness of web pages through new tags and APIs, such as and tags, improving user experience and SEO effects, but need to pay attention to compatibility issues.

The difference between H5 and HTML5 is: 1) HTML5 is a web page standard that defines structure and content; 2) H5 is a mobile web application based on HTML5, suitable for rapid development and marketing.

The core features of HTML5 include semantic tags, multimedia support, form enhancement, offline storage and local storage. 1. Semantic tags such as, improve code readability and SEO effect. 2. Multimedia support simplifies the process of embedding media content through and tags. 3. Form Enhancement introduces new input types and verification properties, simplifying form development. 4. Offline storage and local storage improve web page performance and user experience through ApplicationCache and localStorage.

HTML5isamajorrevisionoftheHTMLstandardthatrevolutionizeswebdevelopmentbyintroducingnewsemanticelementsandcapabilities.1)ItenhancescodereadabilityandSEOwithelementslike,,,and.2)HTML5enablesricher,interactiveexperienceswithoutplugins,allowingdirectembe

Advanced tips for H5 include: 1. Use complex graphics to draw, 2. Use WebWorkers to improve performance, 3. Enhance user experience through WebStorage, 4. Implement responsive design, 5. Use WebRTC to achieve real-time communication, 6. Perform performance optimization and best practices. These tips help developers build more dynamic, interactive and efficient web applications.

H5 (HTML5) will improve web content and design through new elements and APIs. 1) H5 enhances semantic tagging and multimedia support. 2) It introduces Canvas and SVG, enriching web design. 3) H5 works by extending HTML functionality through new tags and APIs. 4) Basic usage includes creating graphics using it, and advanced usage involves WebStorageAPI. 5) Developers need to pay attention to browser compatibility and performance optimization.

H5 brings a number of new functions and capabilities, greatly improving the interactivity and development efficiency of web pages. 1. Semantic tags such as enhance SEO. 2. Multimedia support simplifies audio and video playback through and tags. 3. Canvas drawing provides dynamic graphics drawing tools. 4. Local storage simplifies data storage through localStorage and sessionStorage. 5. The geolocation API facilitates the development of location-based services.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
