1. <i id='bpIoU'><tr id='bpIoU'><dt id='bpIoU'><q id='bpIoU'><span id='bpIoU'><b id='bpIoU'><form id='bpIoU'><ins id='bpIoU'></ins><ul id='bpIoU'></ul><sub id='bpIoU'></sub></form><legend id='bpIoU'></legend><bdo id='bpIoU'><pre id='bpIoU'><center id='bpIoU'></center></pre></bdo></b><th id='bpIoU'></th></span></q></dt></tr></i><div id='bpIoU'><tfoot id='bpIoU'></tfoot><dl id='bpIoU'><fieldset id='bpIoU'></fieldset></dl></div>
      <tfoot id='bpIoU'></tfoot>

      <small id='bpIoU'></small><noframes id='bpIoU'>

    2. <legend id='bpIoU'><style id='bpIoU'><dir id='bpIoU'><q id='bpIoU'></q></dir></style></legend>

        • <bdo id='bpIoU'></bdo><ul id='bpIoU'></ul>

        可以使用 xhrFields 将 onprogress 功能添加到 jQuery.

        时间:2023-10-15
        • <small id='hL1ru'></small><noframes id='hL1ru'>

          <i id='hL1ru'><tr id='hL1ru'><dt id='hL1ru'><q id='hL1ru'><span id='hL1ru'><b id='hL1ru'><form id='hL1ru'><ins id='hL1ru'></ins><ul id='hL1ru'></ul><sub id='hL1ru'></sub></form><legend id='hL1ru'></legend><bdo id='hL1ru'><pre id='hL1ru'><center id='hL1ru'></center></pre></bdo></b><th id='hL1ru'></th></span></q></dt></tr></i><div id='hL1ru'><tfoot id='hL1ru'></tfoot><dl id='hL1ru'><fieldset id='hL1ru'></fieldset></dl></div>
        • <tfoot id='hL1ru'></tfoot>

            <tbody id='hL1ru'></tbody>

            <bdo id='hL1ru'></bdo><ul id='hL1ru'></ul>

              1. <legend id='hL1ru'><style id='hL1ru'><dir id='hL1ru'><q id='hL1ru'></q></dir></style></legend>
                1. 本文介绍了可以使用 xhrFields 将 onprogress 功能添加到 jQuery.ajax() 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  这里建议:https://gist.github.com/HenrikJoreteg/2502497,我正在尝试将 onprogress 功能添加到我的 jQuery.ajax() 文件上传中.上传工作正常,并且 onprogress 事件正在触发,但不像我预期的那样 - 不是在某个时间间隔重复触发,而是在上传完成时只触发一次.有没有办法指定onprogress刷新的频率?或者,我是否正在尝试做一些无法做到的事情?这是我的代码:

                  As suggested here: https://gist.github.com/HenrikJoreteg/2502497, I'm trying to add onprogress functionality to my jQuery.ajax() file upload. The upload works fine, and the onprogress event is firing, but not as I expected--instead of firing repeatedly at some time interval, it's firing only once, when the upload has completed. Is there a way to specify the frequency of onprogress refreshes? Or, am I trying to do something that can't be done? Here's my code:

                      $.ajax(
                      {
                          async: true,
                          contentType: file.type,
                          data: file,
                          dataType: 'xml',
                          processData: false,
                          success: function(xml)
                          {
                              // Do stuff with the returned xml
                          },
                          type: 'post',
                          url: '/fileuploader/' + file.name,
                          xhrFields:
                          {
                              onprogress: function(progress)
                              {
                                  var percentage = Math.floor((progress.total / progress.totalSize) * 100);
                                  console.log('progress', percentage);
                                  if (percentage === 100)
                                  {
                                      console.log('DONE!');
                                  }
                              }
                          }
                      });
                  


                  嗯,已经好几年了.我重新审视了这一点,并使用 GetFree 的答案,将我的代码更新为以下内容:


                  Well, it's been a few years. I revisited this, and using GetFree's answer, I updated my code to the following:

                  $('#file_input').change(function()
                  {
                      var file = this.files[0];
                      $('#upload_button').click(funtion(e)
                      {
                          req = new XMLHttpRequest();
                          req.upload.addEventListener('progress', updateProgress, false);
                          req.addEventListener('load', transferComplete, false);
                          var url  = 'https://my.url'; 
                          req.open('POST', url, true);
                          req.setRequestHeader('Content-Type', myFileType);
                          req.setRequestHeader('Content-Length', myFileLength);
                          req.send(file);
                      });
                  );
                  function updateProgress(e)
                  {
                      var percent = Math.floor(e.loaded / e.total * 100);
                      console.log("percent = " + percent);
                  }
                  function transferComplete(e)
                  {
                      console.log("transfer complete");
                  }
                  

                  我已将 GetFree 的帖子标记为已接受的答案.抱歉耽搁了.

                  I have marked GetFree's post as the accepted answer. Sorry for the delay.

                  推荐答案

                  简答:
                  不,你不能使用 xhrFields 做你想做的事.

                  长答案:

                  XmlHttpRequest 对象中有两个进度事件:

                  There are two progress events in a XmlHttpRequest object:

                  • 响应进度(XmlHttpRequest.onprogress)
                    这是浏览器从服务器下载数据的时候.

                  • The response progress (XmlHttpRequest.onprogress)
                    This is when the browser is downloading the data from the server.

                  请求进度 (XmlHttpRequest.upload.onprogress)
                  这是浏览器向服务器发送数据(包括 POST 参数、cookie 和文件)的时候

                  The request progress (XmlHttpRequest.upload.onprogress)
                  This is when the browser is sending the data to the server (including POST parameters, cookies, and files)

                  在您的代码中,您使用的是响应进度事件,但您需要的是请求进度事件.这就是你的做法:

                  In your code you are using the response progress event, but what you need is the request progress event. This is how you do it:

                  $.ajax({
                      async: true,
                      contentType: file.type,
                      data: file,
                      dataType: 'xml',
                      processData: false,
                      success: function(xml){
                          // Do stuff with the returned xml
                      },
                      type: 'post',
                      url: '/fileuploader/' + file.name,
                      xhr: function(){
                          // get the native XmlHttpRequest object
                          var xhr = $.ajaxSettings.xhr() ;
                          // set the onprogress event handler
                          xhr.upload.onprogress = function(evt){ console.log('progress', evt.loaded/evt.total*100) } ;
                          // set the onload event handler
                          xhr.upload.onload = function(){ console.log('DONE!') } ;
                          // return the customized object
                          return xhr ;
                      }
                  });
                  

                  xhr 选项参数必须是返回原生 XmlHttpRequest 对象以供 jQuery 使用的函数.

                  The xhr option parameter must be a function that returns a native XmlHttpRequest object for jQuery to use.

                  这篇关于可以使用 xhrFields 将 onprogress 功能添加到 jQuery.ajax() 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 JavaScript 中拦截 fetch() API 请求和响应 下一篇:跨站 XMLHttpRequest

                  相关文章

                  最新文章

                2. <small id='Ignry'></small><noframes id='Ignry'>

                    <legend id='Ignry'><style id='Ignry'><dir id='Ignry'><q id='Ignry'></q></dir></style></legend>

                  1. <i id='Ignry'><tr id='Ignry'><dt id='Ignry'><q id='Ignry'><span id='Ignry'><b id='Ignry'><form id='Ignry'><ins id='Ignry'></ins><ul id='Ignry'></ul><sub id='Ignry'></sub></form><legend id='Ignry'></legend><bdo id='Ignry'><pre id='Ignry'><center id='Ignry'></center></pre></bdo></b><th id='Ignry'></th></span></q></dt></tr></i><div id='Ignry'><tfoot id='Ignry'></tfoot><dl id='Ignry'><fieldset id='Ignry'></fieldset></dl></div>
                  2. <tfoot id='Ignry'></tfoot>
                        <bdo id='Ignry'></bdo><ul id='Ignry'></ul>