首页 > 解决方案 > 进行并行下载时,React-Native-fs 下载速度慢

问题描述

下载单个文件时我能够有很好的速度。但是当我开始下载多个文件时,下载速度太差了。我开始这是我的代码

import { DocumentDirectoryPath,  exists, mkdir, readDir, downloadFile,  writeFile , readFile} from 'react-native-fs'
function download(fileId, fileUrl){ 
    let path = DocumentDirectoryPath+'/'+'Learning/'+fileId; 
          let zipPath =DocumentDirectoryPath+'/'+'Learning/'+fileId+"/Compressed.zip";
          let exist = await exists(path);
          if(exist === false){
    
            await  mkdir(path);
            console.log("path created", path)
          }
          
    //alert("======"+fileUrl)
        const downloadPromise = downloadFile({
          fromUrl: fileUrl  ,
          toFile: zipPath, 
          progress: function ({contentLength, jobId, bytesWritten}) {
            if (jobId === downloadPromise.jobId) {
              progressCallback(bytesWritten / contentLength);
              console.log( "\b",bytesWritten / contentLength)
            }
          },
        });
        const downloadResult = await downloadPromise.promise;}

然后像这样开始多次下载:

let files = [{url: "https//....", id: "y6HS6"}, {url: "https//....", id: "y6HghhS6"}]
files.map(item =>{
download(item.id, item.url)
})

标签: javascriptreact-nativereact-native-fs

解决方案


推荐阅读