简体   繁体   English

如何获取ajax发布中文件上传的状态

[英]how to get the status of file upload in ajax post

How the get the status of uploaded files when user clicks on cancel while uploading multiple files using ajax call. 用户使用ajax调用上传多个文件单击“取消”时如何获取上传文件的状态。

This way im calling the ajax request to upload files: 这样我调用ajax请求来上传文件:

 var request = $.ajax({
     url: 'files.php',
     type: 'POST',
     data: data,
     cache: false,
     contentType: false,
     processData: false,
     beforeSend: function () {
         $(".progressbar").show();
     },
     xhr: function () {
         var xhr = $.ajaxSettings.xhr();
         if (xhr.upload) {
             xhr.upload.addEventListener('progress', showProgress, false);
         }
         return xhr;
     },
     success: function (data) {
         if (percentComplete <= 100) {
             $('#pb div').animate({
                 width: '100%'
             }, {
                 step: function (now) {
                     $(this).text(Math.round(now) + '%');
                 },
                 duration: 10
             });
         }
         $('#uplcomp').append(data);
     }
 });

If user clicks on cancel button im doing this: 如果用户单击“取消”按钮,我会这样做:

request.abort();

As the above statement just abort the ajax request , im not getting any response like how many files are uploaded, how much mb uploaded etc.... 由于上述声明只是中止了ajax请求,因此我没有得到任何响应,例如上传了多少文件,上传了多少mb等。

Can anyone help me on this? 谁可以帮我这个事?

I don't think that there is default way for your question. 我认为您的问题没有默认的方式。 The information you're looking for - how many files are uploaded - is not stored in the XMLHttpRequest object by my knowlegde. 我要知道的是,您要查找的信息(上传了多少文件)没有存储在XMLHttpRequest对象中。

I suppose (but never tried it) that you can create somehting custom for it. 我想(但从未尝试过)您可以为其创建某种定制。 High level analysis: keep track of your upload progress server side. 高级分析:跟踪您的上传进度服务器端。 Store some info in your database regarding the upload: amount of files to process, amount of files processed, date/time, unique upload/session ID. 在您的数据库中存储有关上载的一些信息:要处理的文件数量,已处理的文件数量,日期/时间,唯一的上载/会话ID。 When you abort the the upload, or it fails you can retrieve the information, based on a unique ID, about the upload process via ajax from your DB and present it in the UI. 当您中止上载或上传失败时,您可以基于唯一ID,通过ajax从数据库中检索有关上载过程的信息,并将其显示在UI中。

Note: If the request has already been sent to the server then the server will process the request even if we abort the request but the client will not wait for/handle the response. 注意:如果请求已经发送到服务器,那么即使我们中止请求,服务器也将处理该请求,但客户端不会等待/处理响应。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM