Detailed explanation of upload. onprogress case of Ajax file upload progress monitoring

  • 2021-11-14 04:51:10
  • OfStack

Implementation of $. 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() {
        //  The trigger event when the user clicks the "Open" button of the pop-up layer after selecting the file is: change
        $('input').on('change', function() {
          // 1. Collect file data 
          let myfile = $('input')[0].files[0]
          let formdata = new FormData()
          formdata.append('file_data', myfile)

          // 2. Initiate ajax Request 
          $.ajax({
            url: 'http://127.0.0.1:3001/uploadFile',
            type: 'post',
            data: formdata,
            processData: false,
            contentType: false,
            xhr: function() {
              let newxhr = new XMLHttpRequest()
              //  Add listening for file uploads 
              // onprogress: Progress monitoring event, as long as the progress of uploading files has changed, it will automatically trigger this event 
              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>

Native implementation:


<!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() {
        //  The trigger event when the user clicks the "Open" button of the pop-up layer after selecting the file is: change
        $('input').on('change', function() {
          // 1. Collect file data 
          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')

          //  Details 1 File upload, if using fromdata, Do not set the request header 
          xhr.upload.onprogress = function(e) {
            console.log(e)
            let percent = (e.loaded / e.total) * 100 + '%'
            $('div').css('width', percent)
          }
          //  Details 2 : send Can be passed directly in formdata
          xhr.send(formdata)
        })
      })
    </script>
  </body>
</html>


Related articles: