Alamofire скачать файл с проблемой прогресса

Я хочу скачать файл с сервера с прогрессом. Вот код, который я пробовал:

let destination: (NSURL, NSHTTPURLResponse) -> (NSURL) = {
(temporaryURL, response) in
if let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as? NSURL {
let path = directoryURL.URLByAppendingPathComponent(response.suggestedFilename!)
return path
}
return temporaryURL
}

Alamofire.download(.GET, fileUrls[button.tag], destination: destination)
.progress { _, totalBytesRead, totalBytesExpectedToRead in
dispatch_async(dispatch_get_main_queue()) {
println("\(Float(totalBytesRead)) - \(Float(totalBytesExpectedToRead))")

if totalBytesRead == totalBytesExpectedToRead {
println("******************************")
println("finished")
println("******************************")
}
}
}
.response { (_, _, data, error) in
println(data)
println(error)
}

Но totalBytesExpectedToRead всегда -1. Я искал эту проблему и обнаружил, что Content-Length на стороне сервера не установлен.

Я пытался установить его, но это не похоже на работу:

$attachment_location = $_GET["filepath"];
if (file_exists($attachment_location)) {
header('Content-Description: File Transfer');
header('Content-Type:' . mime_content_type($attachment_location));
header('Content-Disposition: attachment; filename='.basename($attachment_location));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($attachment_location));
readfile($attachment_location);
die();
} else {
die("Error: File not found.");
}

Может кто-нибудь сказать мне, что я делаю не так? Благодарю.

0

Решение

Решено путем изменения кода на это:

$attachment_location = $_GET["filepath"];
if (file_exists($attachment_location)) {
header('Content-Description: File Transfer');
header('Content-Type:' . mime_content_type($attachment_location));
header('Content-Disposition: attachment; filename='.basename($attachment_location));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Encoding: chunked');
header('Content-Length: ' . filesize($attachment_location), true);
readfile($attachment_location);
die();
} else {
die("Error: File not found.");
}
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector