简体   繁体   English

使用alamofire下载不完整的文件

[英]Incomplete files download with alamofire

I have a list of 60 remote file (images) and I want to download all of them but when I try just download 15 images here my code 我有60个远程文件(图像)的列表,我想下载所有这些文件,但是当我尝试在此处下载15个图像时,我的代码

    let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)

    for var i = 1; i < 61; i++ {

        Alamofire.download(.GET, "https://domain.com/folder/\(i).jpg", destination: destination)
            .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
                dispatch_async(dispatch_get_main_queue()) {
                    print("Total bytes read on main queue: \(totalBytesRead)")
                }
            }
            .response { _, _, _, error in
                if let error = error {
                    print("Failed with error: \(error)")
                } else {
                    print("Downloaded file successfully")
                }
        }

    }

any idea??? 任何想法???

func doDownload(i:int)
{
    if(i > 60)
    {
        return;
    }
    let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)

    Alamofire.download(.GET, "https://domain.com/folder/\(i).jpg", destination: destination)
        .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
            dispatch_async(dispatch_get_main_queue()) {
                print("Total bytes read on main queue: \(totalBytesRead)")
            }
        }
        .response { _, _, _, error in
            if let error = error {
                print("Failed with error: \(error)")
            } else {
                print("Downloaded file successfully")
                doDownload(i + 1); //If you only want to download on success do it here
            }
            //Otherwise doDownload(i + 1)here;
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM