Ajax 文件上傳進度監聽之upload.onprogress案例詳解

$.ajax實現

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <script src="./libs/jquery/jquery.js"></script>
    <style>
      div {
        width: 0%;
        height: 20px;
        background-color: #f00;
        /* transition: all 0.2s; */
      }
    </style>
  </head>
  <body>
    <div></div>
    <input type="file" />
    <script>
      $(function() {
        // 用戶選擇好文件之後單擊彈出層的“打開”按鈕的觸發事件是:change
        $('input').on('change', function() {
          // 1.收集文件數據
          let myfile = $('input')[0].files[0]
          let formdata = new FormData()
          formdata.append('file_data', myfile)

          // 2.發起ajax請求
          $.ajax({
            url: 'http://127.0.0.1:3001/uploadFile',
            type: 'post',
            data: formdata,
            processData: false,
            contentType: false,
            xhr: function() {
              let newxhr = new XMLHttpRequest()
              // 添加文件上傳的監聽
              // onprogress:進度監聽事件,隻要上傳文件的進度發生瞭變化,就會自動的觸發這個事件
              newxhr.upload.onprogress = function(e) {
                console.log(e)
                let percent = (e.loaded / e.total) * 100 + '%'
                $('div').css('width', percent)
              }
              return newxhr
            },
            success: function(res) {
              console.log(res)
            },
            dataType: 'json'
          })
        })
      })
    </script>
  </body>
</html>

原生實現:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <script src="./libs/jquery/jquery.js"></script>
    <style>
      div {
        width: 0%;
        height: 20px;
        background-color: #f00;
        /* transition: all 0.2s; */
      }
    </style>
  </head>
  <body>
    <div></div>
    <input type="file" />
    <script>
      $(function() {
        // 用戶選擇好文件之後單擊彈出層的“打開”按鈕的觸發事件是:change
        $('input').on('change', function() {
          // 1.收集文件數據
          let myfile = $('input')[0].files[0]
          let formdata = new FormData()
          formdata.append('file_data', myfile)

          let xhr = new XMLHttpRequest()

          xhr.open('post', 'http://127.0.0.1:3001/uploadFile')

          // 細節1:文件上傳,如果使用fromdata,則不要設置請求頭
          xhr.upload.onprogress = function(e) {
            console.log(e)
            let percent = (e.loaded / e.total) * 100 + '%'
            $('div').css('width', percent)
          }
          // 細節2:send中可以直接傳遞formdata
          xhr.send(formdata)
        })
      })
    </script>
  </body>
</html>

到此這篇關於Ajax 文件上傳進度監聽之upload.onprogress案例詳解的文章就介紹到這瞭,更多相關Ajax 文件上傳進度監聽之upload.onprogress內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: