跨域上传进度条在Firefox中不起作用



我有以下代码,这是一个带有进度条的跨域上传程序(example.com)。

    $.ajax({
                    url: 'http://x.x.x.x:8080/.../uploader.php',
                    type: 'post',               
                    crossDomain: true,
                    data: formData,         
                    xhr: function() {               
                        var myXhr = $.ajaxSettings.xhr();               
                        if(myXhr.upload){                       
                            myXhr.upload.addEventListener('progress',progress, false);                      
                        }
                        return myXhr;
                    },
                    contentType: false,
                    processData: false,                     
                    success: function(dataupl) {
                        var json = JSON.parse(dataupl);
                        $('#uploadername').html(json.success);              }
                });
function progress(e){
                if(e.lengthComputable){
                    var progressbox     = $('#progressbox');
                    var progressbar     = $('#progressbar');
                    var statustxt       = $('#statustxt');
                    var completed       = '0%';
                    progressbox.show(250);                      
                    var max = e.total;
                    var current = e.loaded;
                    var Percentage = Math.floor((current * 100)/max);
                    //Progress bar
                    progressbar.width(Percentage + '%') //update progressbar percent complete
                    statustxt.html(Percentage + '%'); //update status text
                    if(Percentage>55)
                        {
                            statustxt.css('color','#fff'); //change status text to white after 50%
                        }                   
                    //console.log(Percentage);
                    if(Percentage >= 100) {
                       progressbox.hide(250);
                    }
                }  
            }

和服务器上的php(x.x.x.x):

<?php
header('Access-Control-Allow-Origin: http://example.com');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 6400');
$target_dir = 'uploads/';
//$target_file = $_FILES['file']['name'];
// Sanitize the filename
$target_file = basename(preg_replace('/[^a-zA-Z0-9.-s+]/', '', html_entity_decode($_FILES['file']['name'], ENT_QUOTES, 'UTF-8')));
    if (move_uploaded_file($_FILES['file']['tmp_name'], $target_dir . $target_file)) {
        $md5 = md5(mt_rand());
        chmod($target_dir . $target_file, 0777); //change permission 
        rename($target_dir . $target_file, $target_dir . $target_file . '.' . $md5);
        $success = File <b>". $target_file . "</b> has been uploaded succesfully.";
        $name = basename($target_file);
        $dataupl = array(
            'success' => $success,
            'name' => $name,
            'md5'    => $md5,       
        );  
        print(json_encode($dataupl));
    } else {
        $success = "Error.";
        $dataupl = array(
            'success' => $success,
        );  
        print(json_encode($dataupl));
    }
?> 

进度条在Chrome和IE11上运行成功,但在Mozilla上不起作用。如果我评论出这个代码:

xhr: function() {               
                            var myXhr = $.ajaxSettings.xhr();               
                            if(myXhr.upload){                       
                                myXhr.upload.addEventListener('progress',progress, false);                      
                            }
                            return myXhr;
                        },

每个浏览器都可以上传(没有进度条)。

知道为什么这个代码不能和Firefox一起使用吗?(对不起我的英语)。

在ajax调用之前定义"函数进度(e)",并工作。

相关内容

  • 没有找到相关文章

最新更新