- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <link href="../resources/css/common.css" rel="stylesheet" />
- <script src="../resources/js/jquery-2.1.4.js"></script>
-
- </head>
-
- <body>
- <h2>HTML5异步上传文件,带进度条</h2>
- <form method="post" enctype="multipart/form-data">
- 其他需要提交的信息:<input type="text" name="otherInfo"/><br/><br/>
- 选择要上传的文件:<br/>
- <input type="file" name="file" /><span></span><br/>
- <input type="file" name="file" /><span></span><br/>
- </form>
-
- <br/><br/>
- <input type="button" value="上传吧" onclick="upload()"/>
- <br/><br/>
- 上传进度:<progress></progress><br/>
- <p id="progress">0 bytes</p>
- <p id="info"></p>
- </body>
- <script>
- var totalSize = 0;
- $(':file').change(function() {
- var file = this.files[0];
- name = file.name;
- size = file.size;
- type = file.type;
- url = window.URL.createObjectURL(file);
- $(this).next().html("文件名:" + name + " 文件类型:" + type + " 文件大小:" + size + " url: " + url);
- totalSize += size;
- $("#info").html("总大小: " + totalSize + "bytes");
- });
- function upload() {
- var formData = new FormData($('form')[0]);
- $.ajax({
- url: "http://localhost:8080/MyJavaStudio/servlet/file/upload",
- type: "POST",
- data: formData,
- xhr: function(){
- myXhr = $.ajaxSettings.xhr();
- if(myXhr.upload){
- myXhr.upload.addEventListener('progress',progressHandlingFunction, false);
- }
- return myXhr;
- },
- success: function(result){
- $("#result").html(result.data);
- },
- contentType: false,
- processData: false
- });
- }
- function progressHandlingFunction(e) {
- if (e.lengthComputable) {
- $('progress').attr({value : e.loaded, max : e.total});
- var percent = e.loaded/e.total*100;
- $('#progress').html(e.loaded + "/" + e.total+" bytes. " + percent.toFixed(2) + "%");
- }
- }
- </script>
- </html>
posted @
2016-10-25 09:48
慶哥
阅读(
4532)
评论()
编辑
收藏
举报