| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- var size = 0;
- $("#upload").click(upload);
- // 绑定所有type=file的元素的onchange事件的处理函数
- $(':file').change(function() {
- var file = this.value;
- if (file) {
- name = file.name;
- size = file.size;
- type = file.type;
- // url = window.URL.createObjectURL(file);
- }
- });
- /**
- * 上传文件
- */
- function upload() {
- $('#result').html("");
- var value = document.getElementById("file").value;
- if (!value) {
- $('#progressDiv').css("display", "hidden");
- $("#result").html("文件为空,无法进行上传!");
- return;
- }
- // 创建FormData对象,初始化为form表单中的数据
- var formData = new FormData($('form')[0]);
- // 显示进度
- $('#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);
- },
- 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)
- + "%");
- }
- }
|