index.html 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Document</title>
  5. <script src="http://www.1473.cn/uform.js"></script>
  6. </head>
  7. <style>
  8. form p span{
  9. margin-left: 20px;
  10. }
  11. </style>
  12. <body>
  13. <form action="">
  14. <input type="file" id="uploadfile" >
  15. <button id="uploadbtn">开始上传</button>
  16. <p>文件名<span id="filename">无</span></p>
  17. <p>大小<span id="filesize">0</span></p>
  18. <p>进度<span id="progress">0%</span></p>
  19. </form>
  20. </body>
  21. <script src="./splitUpload.js"></script>
  22. <script>
  23. var els = {
  24. uploadfile: $("#uploadfile")[0],
  25. uploadbtn: $("#uploadbtn")[0],
  26. filename: $("#filename")[0],
  27. filesize: $("#filesize")[0],
  28. progress: $("#progress")[0]
  29. }
  30. // 当前上传文件的file对象
  31. var currentFile = null;
  32. // 暂停的标签
  33. // var stopSign = true;
  34. // 开始上传事件
  35. uploadbtn.onclick = function(e){
  36. e.preventDefault();
  37. if(!currentFile)
  38. return;
  39. // 开始上传
  40. beginUpload(currentFile)
  41. }
  42. // 选择文件事件
  43. uploadfile.onchange = function(){
  44. var file = uploadfile.files[0], name, size;
  45. if(!file || !file.name)
  46. return;
  47. name = file.name;
  48. size = file.size;
  49. els.filename.innerHTML = name;
  50. els.filesize.innerHTML = size > 1024
  51. ? size / 1024 > 1024
  52. ? size / (1024 * 1024) > 1024
  53. ? (size / (1024 * 1024 * 1024)).toFixed(2) + 'GB'
  54. : (size / (1024 * 1024)).toFixed(2) + 'MB'
  55. : (size / 1024).toFixed(2) + 'KB'
  56. : (size).toFixed(2) + 'B';
  57. // 设置当前文件
  58. currentFile = file;
  59. }
  60. /**
  61. * 开始上传
  62. * @param {File} file 上传的文件
  63. */
  64. function beginUpload(file){
  65. splitUpload(file, function(thunkTo, totalSize) { // success
  66. progress.innerHTML = (100*thunkTo/totalSize).toFixed(2) +'%';
  67. if(thunkTo === totalSize)
  68. progress.innerHTML += ' 写入成功';
  69. }, function(res) { // error
  70. progress.innerHTML += ' 写入失败';
  71. })
  72. }
  73. </script>
  74. </html>