ajax上传下载实现进度条

$$.ajax({
    method: 'POST',
    url: './upload',
    contentType: false,
    cache: false,
    processData: false,
    data: formData,
    beforeSend: function (XMLHttpRequest) {

        //Upload progress
        XMLHttpRequest.upload.addEventListener("progress", function (evt) {
            if (evt.lengthComputable) {
                var percentComplete = Math.round((evt.loaded * 100) / evt.total);
                //Do something with upload progress
                console.log('Uploaded percent', percentComplete);
                $$("#progressBar").css("width", percentComplete + "%")
            }
        }, false);
        // //Download progress
        // XMLHttpRequest.addEventListener( "progress", function ( evt )
        // {
        //     if ( evt.lengthComputable )
        //     {
        //         var percentComplete = Math.round( (evt.loaded * 100) / evt.total );
        //         //Do something with download progress
        //         console.log( 'Downloaded percent', percentComplete );
        //     }
        // }, false );
    },
    success: function (data) {
        console.log(data);
    }
});

你可能感兴趣的:(前端)