I want to download a file from server with progress. Here is the code I've tried:
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)
}
But totalBytesExpectedToRead is always -1. I've searched this issue and found that Content-Length on server side isn't set.
I've tried to set it but it doesn't seem to work:
$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.");
}
Can some one tell me what I'm doing wrong? Thanks.
Solved by changing the code to this:
$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.");
}
Related
I created a simple file to download files.
I have the following code:
function DownloadFile($file) { // $file = include path
if(file_exists($file)) {
header('HTTP/1.0 200 OK', true, 200);
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
ob_clean();
flush();
readfile($file);
exit;
}
}
The problem is if I try to download files over 40-50MB the file is downloaded blank(0KB) but if I try below 40MB it is working perfectly.
What is the problem?
Like explained in this article https://www.sitepoint.com/community/t/php-file-size-download-limit/6541 you need to set
memory_limit = 128M
post_max_size = 300M
in php.ini file
The solution:
function DownloadFile($file) { // $file = include path
if(file_exists($file)) {
header('HTTP/1.0 200 OK', true, 200);
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: chunked');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
ob_end_flush();
if ($fd = fopen ($file, "r")) {
set_time_limit(0);
ini_set('memory_limit', '1024M');
while(!feof($fd)) {
echo fread($fd, 4096);
flush();
}
}
}
}
Good morning,
I'm working on serving a zip file through the PHP headers, which downloads, but every time I try to extract it shows that the zip is corrupt... I need a 2nd pair of eyes to look over this... what am I missing? Thank you so much!
$file_download = 'example';
if (isset($file_download)) {
$file = 'path/to/file/'.$file_download.'.zip';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
} else {
echo 'File does not exist!';
}
}
When I try to download a file, it downloads but without content, i.e., its size is 0 bytes.
I am using this code, what I am doing wrong?
$fullPath = "/home/yakar/www/files/gpx/".$file;
if (file_exists($fullPath)) {
// setting headers
header('Content-Description: File Transfer');
header('Content-Type: '.$type);
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
} else {
die('File does not exist');
}
Everytime I use the code below to stream an image file to me the file comes back as corrupt. The image file most certainly is not corrupt though and is fine when I open it through my ftp. Am I missing something?
$fileTry = imagecreatefromstring(file_get_contents($file_path));
if($fileTry === false)
{
die('no file');
}
else
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file_path));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_path));
readfile($file_path);
exit;
}
I am trying to download image file in CI but getting error when I open download image file. Need help :(
$file_name = $_GET['file_name'];
$file_path = "./ups_printimages/".$file_name;
if(file_exists($file_path))
{
$this->load->helper('download');
$data = file_get_contents($file_path); // Read the file's contents
$name = 'ups_label.png';
force_download($name, $data);
}
else
{
echo "file does not exist";
}
GOD. Found out the solution :)
if(!file)
{
File doesn't exist, output error
die('file not found');
}
else
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
ob_clean();
flush();
readfile($file);
exit;
}
Use header to force download. Use the code below
$file_name = $_GET['file_name'];
$file_path = "./ups_printimages/".$file_name;
if(file_exists($file_path))
{
header('Content-Type: image/jpeg');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_name) . "\"");
readfile($file_path);
}
else
{
echo "file does not exist";
}
Hope this helps yoiu