app.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var size = 0;
  2. // 绑定所有type=file的元素的onchange事件的处理函数
  3. $(':file').change(function() {
  4. var file = this.file;
  5. name = file.name;
  6. size = file.size;
  7. type = file.type;
  8. url = window.URL.createObjectURL(file);
  9. });
  10. function upload() {
  11. // 创建FormData对象,初始化为form表单中的数据
  12. var formData = new FormData($('form')[0]);
  13. formData.append('userName', 'UAS');
  14. // 显示进度
  15. $('#progressDiv').css("display", "block");
  16. // $('#progressDiv').toggle();
  17. // ajax异步上传
  18. $.ajax({
  19. url : "upload",
  20. type : "POST",
  21. data : formData,
  22. xhr : function() {
  23. myXhr = $.ajaxSettings.xhr();
  24. if (myXhr.upload) {
  25. // 绑定progress事件的回调函数
  26. myXhr.upload.addEventListener('progress',
  27. progressHandlingFunction, false);
  28. }
  29. return myXhr;
  30. },
  31. success : function(result) {
  32. $("#result").html(result.data);
  33. },
  34. contentType : false,
  35. processData : false
  36. });
  37. }
  38. // 上传进度回调函数:
  39. function progressHandlingFunction(e) {
  40. if (e.lengthComputable) {
  41. $('progress').attr({
  42. value : e.loaded,
  43. max : e.total
  44. }); // 更新数据到进度条
  45. var percent = e.loaded / e.total * 100;
  46. $('#progress').html(
  47. e.loaded + "/" + e.total + " bytes. " + percent.toFixed(2)
  48. + "%");
  49. }
  50. }