小编dco*_*llo的帖子

可以使用xhrFields将onprogress功能添加到jQuery.ajax()吗?

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

$.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!');
            }
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

ajax jquery xmlhttprequest progress

25
推荐指数
1
解决办法
4万
查看次数

标签 统计

ajax ×1

jquery ×1

progress ×1

xmlhttprequest ×1