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

时间:2015-12-14 10:47:19

标签: php jquery ajax cross-domain

我有以下代码,这是一个带有进度条的跨域上传器(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? (抱歉我的英文)。

1 个答案:

答案 0 :(得分:-1)

在ajax调用之前定义“函数进度(e)”,并且有效。