Titanium 钛:[错误]:必须将连接设置为在发送之前打开()

Titanium 钛:[错误]:必须将连接设置为在发送之前打开(),titanium,titanium-mobile,Titanium,Titanium Mobile,我试图下载多个文件,但由于某些原因,它只下载一个文件 我的代码: for (var fl = 0; fl < fileurls.length; fl++){ var fname = fileurls[fl].substr(fileurls[fl].lastIndexOf('/')+1); var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, fname);

我试图下载多个文件,但由于某些原因,它只下载一个文件

我的代码:

for (var fl = 0; fl < fileurls.length; fl++){
    var fname = fileurls[fl].substr(fileurls[fl].lastIndexOf('/')+1);
    var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, fname);

    if(file.exists()) {  
        dlurls.push(fname);
        Ti.API.info('File exists, adding: ' + fname)
    }

    if(!file.exists()) {  
        Ti.API.info('File does not exist, downloading: ' + fname)
        modalview.show();
        ind.show();
        c.open('GET',fileurls[fl]);
        ind.message = 'Downloading: ' + fname;
        c.file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, fname);
        c.onload = function(e)
        {
            dlurls.push(fname);
        };
        c.send();
    } 

    Ti.API.info('Total files: ' + fileurls.length + ' Downloaded files: ' + dlurls.length)
    if (fileurls.length==dlurls.length){
        modalview.hide();
        ind.hide();
    }
}

好的,我所做的就是把整个请求放在一个函数中,然后让oncomplete调用这个函数。现在它起作用了

[INFO] :   File does not exist, downloading: IMG_1643_result.JPG
[INFO] :   Total files: 27 Downloaded files: 0
[INFO] :   File does not exist, downloading: IMG_3883_result.JPG
[INFO] :   Total files: 27 Downloaded files: 0
[INFO] :   File does not exist, downloading: 32103.JPG
[ERROR] :  Must set a connection to OPENED before send()
[INFO] :   Total files: 27 Downloaded files: 0
[INFO] :   File does not exist, downloading: 32142.JPG
[INFO] :   Total files: 27 Downloaded files: 0
[INFO] :   File does not exist, downloading: kale02.JPG
[ERROR] :  Must set a connection to OPENED before send()
[INFO] :   Total files: 27 Downloaded files: 0
[INFO] :   File does not exist, downloading: 3213212.JPG
[INFO] :   Total files: 27 Downloaded files: 0
[INFO] :   File does not exist, downloading: 32123.JPG
[INFO] :   Total files: 27 Downloaded files: 0
[INFO] :   File does not exist, downloading: 32132.jpg
[ERROR] :  Must set a connection to OPENED before send()
...

[INFO] :   Total files: 27 Downloaded files: 1
[INFO] :   ONDATASTREAM1 - PROGRESS: 1
[INFO] :   ONDATASTREAM1 - PROGRESS: 0.602950394153595
[INFO] :   ONDATASTREAM1 - PROGRESS: 1
[INFO] :   ONDATASTREAM1 - PROGRESS: 1.6029504537582397
[INFO] :   ONDATASTREAM1 - PROGRESS: 1.1585098505020142
[INFO] :   ONDATASTREAM1 - PROGRESS: 2.1585099697113037
[INFO] :   ONDATASTREAM1 - PROGRESS: 1
[INFO] :   ONDATASTREAM1 - PROGRESS: 2.2008988857269287
[INFO] :   ONDATASTREAM1 - PROGRESS: 1
[INFO] :   ONDATASTREAM1 - PROGRESS: 1
[INFO] :   ONDATASTREAM1 - PROGRESS: 1
[INFO] :   ONDATASTREAM1 - PROGRESS: 3.8818702697753906
[INFO] :   ONDATASTREAM1 - PROGRESS: 4.881870269775391
[INFO] :   ONDATASTREAM1 - PROGRESS: 1
[INFO] :   ONDATASTREAM1 - PROGRESS: 1
[INFO] :   ONDATASTREAM1 - PROGRESS: 2.251030445098877
[INFO] :   ONDATASTREAM1 - PROGRESS: 3.251030445098877