我尝试使用AF.download()下载文件。
但是我想使用2个或更多请求来完成这项工作。
所以我用下面的代码来做到这一点。
func doDownload2(task: CloudTask) {
let header: HTTPHeaders = [
"Range": "bytes=\(task.chuckStart)-\(task.chunkEnd)"
]
let fileURL = URL(fileURLWithPath: task.localPath)
let destination: DownloadRequest.Destination = { _, _ in
return (fileURL, [.createIntermediateDirectories])
}
let downloadRequest = AF.download(task.cloudPath, method: .get, headers: header, to: destination)
self.lstDownloadRequest.append(downloadRequest)
downloadRequest.response {
response in
self.handledTaskInARound = self.handledTaskInARound + 1
switch response.result {
case .success(_):
if let index = self.lstTasks.firstIndex(of: task) {
print("Remove task \(self.lstTasks[index].chuckStart) - \(self.lstTasks[index].chunkEnd)")
self.lstTasks.remove(at: index)
}
break
case .failure(let error):
print(error)
self.failTaskCount = self.failTaskCount + 1
if self.bCancel {
return
}
break
}
if let statusCode = response.response?.statusCode {
print("DownloadFile() URL: \(task.cloudPath) part(\(task.chuckStart)- \(task.chunkEnd)) Status Code = \(statusCode)")
}
// self.checkTasksStatus()
}
downloadRequest.downloadProgress {
progress in
let dProgress = progress.fractionCompleted
print("URL: \(task.cloudPath) part(\(task.chuckStart)- \(task.chunkEnd)) progress = \(dProgress)")
}
}
我使用此功能下载带有范围标题的文件的两个部分:(0-10485759、10485760-10531374)。
在两个任务中,服务器均返回状态码206。
但是,在完成这2个任务之后,我得到了一个DownloadFileMoveFailed错误(代码= 516)。
我可以将这两部分下载到文件中吗?