黑子
黑子
  • 发布:2021-01-24 00:45
  • 更新:2022-05-10 16:12
  • 阅读:2435

plus.downloader.createDownload下载速度太慢,为什么会这样?

分类:HTML5+

这是我的测试的文件,speed 是用于累加下载量的
applogic是用于延迟显示的
下面是完整代码。麻烦给个答复

测试结果:使用迅雷下载/浏览器下载均能达到1.4M-2。5M左右峰值,可是在downloader下载速度也就100-500kb之间,(一定是同局域网,同WIFI下)请问为什么会慢这么多呢?

图片截图是uniapp先执行下载,迅雷晚了一分钟下载,大约迅雷下载完毕,浏览器上下载完毕了,downloader仅仅才30%的进度左右,为啥会如此之慢呢?

页面代码:
import speed from '@/core/download/speed.js'
import logic from '@/core/applogic.js'
export default {
data() {
return {
sp: undefined
}
},
created() {
const that = this
that.sp = new speed()
that.sp.initlize(1, (_info) => {
if (_info.e === 'ERROR') {

            } else if (_info.e === 'PROCESS') {  
                const that = this  
                logic.shake(function() {  
                    console.log({  
                        percent: that.sp.getPercent(),  
                        speed: that.sp.getSpeed(),  
                        downSize: that.sp.getDownSize()  
                    })  
                }, 1000)  
            } else if (_info.e === 'COMOPLETE') {  

            }  
        })  
    },  
    methods: {  
        down() {  
            console.log('开始下载')  
            const that = this  

            let url = 'http://down.okdown10.com/20210122/17051_809c2b40/神秘博士S04E02.mp4'  
            let dtask = plus.downloader.createDownload(url, {  
                filename: '_doc/dTest/3',  
                retry: 5,  
                retryInterval: 10,  
                timeout: 30  
            }, function(d, status) {  
                console.log(d)  
            })  
            dtask.addEventListener("statechanged", (d, status) => {                   
                const fName = logic.getFileName(d.filename)  
                const tSize = d.totalSize || 0  
                const dSize = d.downloadedSize || 0  
                that.sp.process(fName, dSize, tSize)  
                logic.shake(function() {  
                    console.log({  
                        percent: that.sp.getPercent(),  
                        speed: that.sp.getSpeed(),  
                        downSize: that.sp.getDownSize()  
                    })  
                }, 1000)  
            }, false);  

            dtask.start()  
            console.log('已经启动下载...')  
        }  
    }  
}  

speed.js

/*

  • 下载计算
    */
    const speed = function() {
    let maps = {}
    let downSize = 0
    let totalSpeed = 0
    let len = 0
    let percent = 0
    let action = undefined

    this.initlize = function(l, _act) {
    maps = {}
    downSize = 0
    totalSpeed = 0
    len = l > 0 ? l : 0
    action = _act
    }

    this.competeOne = function(url, size) {
    let temp = maps[url]
    if (temp == undefined) {
    temp = {
    percent: 1,
    size: size,
    tSize: size
    }
    downSize += size
    percent = ((percent len) + 1) / len
    totalSpeed += size
    maps[url] = temp
    } else {
    let newTemp = {
    percent: 1,
    size: size,
    tSize: size
    }
    let speed = newTemp.size - temp.size
    downSize += speed
    totalSpeed += speed
    percent = ((percent
    len) - temp.percent + newTemp.percent) / len
    maps[url] = newTemp
    }

    if (percent == 1) {  
        action({  
            e: 'COMOPLETE',  
            data: {  
                speed: this.getSpeed(),  
                percent: this.getPercent()  
            }  
        })  
    }  

    }

    this.failOne = function() {
    action({
    e: 'ERROR'
    })
    }

    this.process = function(url, dSize, tSize) {
    if (dSize === 0 || tSize === 0) {
    return
    }
    let temp = maps[url]
    if (temp == undefined) {
    temp = {
    percent: dSize / tSize,
    size: dSize,
    tSize: tSize
    }
    downSize += dSize
    totalSpeed += dSize
    percent = ((percent len) + temp.percent) / len
    maps[url] = temp
    } else {
    let newTemp = {
    percent: dSize / tSize,
    size: dSize,
    tSize: tSize
    }
    let speed = newTemp.size - temp.size
    downSize += speed
    totalSpeed += speed
    percent = ((percent
    len) - temp.percent + newTemp.percent) / len
    maps[url] = newTemp
    }
    action({
    e: 'PROCESS',
    data: {
    dSize: downSize,
    //speed: this.getSpeed(),
    percent: this.getPercent()
    }
    })
    }

    this.getSpeed = function() {
    let s = totalSpeed
    //console.log('currnet speed:'+s)
    totalSpeed = 0
    return s
    }

    this.getDownSize = function() {
    return downSize
    }

    this.getPercent = function() {
    return Math.round(percent * 10000) / 10000
    }
    }

export default speed

applogic.js

const applogic = {
shake: false,
shake(action, time) {
if (!applogic.
shake) {
applogic.shake = true
setTimeout(function() {
action()
applogic.
shake = false
}, time)
}
},
sizeFormat: function(bit, type) {
if (!bit) {
return ''
}
if (/^KB$/i.test(type)) {
return (bit / 1024) + 'KB'
} else if (/^MB$/i.test(type)) {
return (bit / 1024 / 1024).toFixed(2) + 'MB'
} else if (/^GB$/i.test(type)) {
return (bit / 1024 / 1024 / 1024).toFixed(2) + 'GB'
}
return bit + 'B'
},
}

export default applogic

2021-01-24 00:45 负责人:无 分享
已邀请:
雨人

雨人

慢到还好,最主要是卡,多文件同时下载,页面会很卡,还是纯nvue

  • 黑子 (作者)

    为什么会卡呢? 多文件下载还好,没感觉卡,UI不要太频繁更新,刚开始我以为是创建下载任务卡,后来发现创建都还好,主要是下载速度太慢太慢,之前我下载m3u8的内容只有几十KB每秒,准备用worker来试试,用一个大文件下载测试,下载本身就慢,也无法提高速度呀,忧伤了...

    2021-01-24 12:22

  • note744917

    回复 黑子: 问题后来怎么解决的

    2021-08-30 15:53

黑子

黑子 (作者)

有管理员吗?能给个答复吗?

  • S***@outlook.com

    2024年这个问题还存在,没有人直到什么原因吗

    2024-02-27 09:58

MonikaChen

MonikaChen

uni.下载不好用吗?

  • 黑子 (作者)

    速度太慢,比同局域网的电脑上迅雷或者浏览器下载慢好几倍,电脑上下载可达3-4M每秒,手机QQ浏览器下载也能达到3-4M每秒。可是使用uniapp这个下载速度最高也就500kb每秒,低的时候几kb,同一个视频,电脑下载几分钟完成,用这个下载下了我几个小时

    2021-01-24 23:37

note744917

note744917 - 我的联系方式:wx: note744917

我的问题跟你一模一样, 同样的资源, 浏览器下载速度5M , 但是plus.downloader.createDownload最高才能到700kb.

1***@qq.com

1***@qq.com

我用这样方法一次下载很多图片,一张一张的下载的,然后我页面就卡得点不动了

  • note744917

    代码看一下, 我一次下载200个视频 不会出现卡, app占用内存也正常. 只不过就是现在速度是很慢 400kb/s. 我wx: note744917

    2021-08-28 14:36

  • 1***@qq.com

    回复 note744917: ok

    2021-08-28 18:20

a***@163.com

a***@163.com

同样问题+1,更新包apk放在阿里云OSS,开启了5G,手机自带浏览器下载10+M/s,十秒不到就下载好了。用这个接口平均70k/s,安装新版本测试个问题都得等几十分钟。
服了。。。。

  • a***@163.com

    换成uni.downloadFile快了很多,现在1M/s左右了,差不多能接受了

    2022-05-10 16:32

要回复问题请先登录注册