Titanium:[ERROR]:必须在send()之前设置连接到OPENED

时间:2014-04-19 18:20:53

标签: 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);

    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();
    }
}

因此for循环解析json,并找到需要下载的所有照片。 它检查文件系统以查看它们是否存在,在哪种情况下它将文件名推送到下载的文件列表。如果该文件不存在,则会启动下载,完成后将文件推送到下载的文件列表。 完成后如果filelist等于下载文件列表,则表示所有文件都在那里。

所以,它确实循环遍历所有文件,但只启动了一次下载?

[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

1 个答案:

答案 0 :(得分:0)

好的,我所做的就是将整个请求放在一个函数中,然后让oncomplete调用函数调用该函数。现在它有效。