| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- var size = 0;
- // 绑定所有type=file的元素的onchange事件的处理函数
- $(':file').change(function() {
- var file = this.file;
- name = file.name;
- size = file.size;
- type = file.type;
- url = window.URL.createObjectURL(file);
- });
- function upload() {
- // 创建FormData对象,初始化为form表单中的数据
- var formData = new FormData($('form')[0]);
- formData.append('userName', 'UAS');
- // 显示进度
- $('#progressDiv').css("display", "block");
- // $('#progressDiv').toggle();
- // ajax异步上传
- $.ajax({
- url : "upload",
- type : "POST",
- data : formData,
- xhr : function() {
- myXhr = $.ajaxSettings.xhr();
- if (myXhr.upload) {
- // 绑定progress事件的回调函数
- 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)
- + "%");
- }
- }
|