I have been developing an iPad application which downloads some PDF files using ASIHTTPRequest. I have been using the progress delegate to display a progress bar for the user showing the file’s download progress. Here’s the ASIHTTPRequest setup code.
request = [ASIHTTPRequest requestWithURL:url];
[request setShouldContinueWhenAppEntersBackground:YES];
[request setDownloadDestinationPath:issue.pdfFilepath];
[request setDownloadProgressDelegate:vc.progressView];
request.showAccurateProgress = YES;
[request setDelegate:self];
[request startAsynchronous];
This has all been working wonderfully reading from my development server (“good server”). However, it’s time to move the server-side of this application to my client’s server (“bad server”). I moved everything over and am still able to download the PDF successfully, but the progress bar does not update until the download is complete (as opposed to throughout the download).
Here’s the PHP I have been using to output the PDF: (Please note that I am also incrementing a view-count, etc in my database based on the requested URL, so I cannot simply access the PDF directly from the app. I need to output the PDF from a PHP script.)
header('Content-type: application/pdf');
header('Content-Disposition:inline filename="downloaded.pdf"');
header('Content-Length: '.remote_filesize($pdfUrl) );
readfile($pdfUrl);
I have been able to determine that the app is not updating the progress bar because the app does not know the PDF’s total size initially. I have determined this by opening both files in Safari.
- Opening the URL for the “good server”, the status bar shows “completed 1.4 of 20.3 MB”.
- Open the URL for the “bad server”, the status bar shows “completed 1.4 MB of ?”.
I have checked, and my remote_filesize() function returns 21331445 (approx 20 MB) on both servers.
So, my conclusion at this point is that for some reason, the “bad server” is not correctly setting the Content-Length header even though I have specified it. Is there any setting in the php.ini file which might be preventing this? Can anyone offer any other suggestions of how to resolve or troubleshoot this further?
Thanks!
Update 5/18/11
Thanks to AJ’s excellent suggestion of inspecting the headers in Firefox, I found that the “bad server” is using gzip compression, and not setting my Conent-Length. Here’s the comparison:
Good Server
Server: Apache
X-Powered-By: PHP/5.3.3
Content-Disposition: inline filename="downloaded.pdf"
Content-Length: 21331445
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: application/pdf
200 OK
Bad Server
Server: Apache
X-Powered-By: PHP/5.2.17
Content-Disposition: inline filename="downloaded.pdf"
Vary: Accept-Encoding
Content-Encoding: gzip
Keep-Alive: timeout=10, max=30
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/pdf
200 OK
I found some articles suggesting to turn gzip off by making sure that “output_buffering = Off” and “zlib.output_compression = Off” in the php.ini file. This is the case though, and my phpinfo() from both servers shows that output_buffering and output_compression are turned off.
We’re certainly on the right track. Any suggestions from here on why this is being gzipped? And how to prevent it?
Thanks!
Final Update: Resolved
I found a forum on the hosting provider’s website where someone explained that gzip has recently been turned on, and detailed how to stop it. http://www.bluehostforum.com/showthread.php?18996-Turning-off-Gzip
Basically, I added the following to my .htaccess file, which turns off the gzip compression. Then, I get the Content-Length header, and my progress bar.
SetEnv no-gzip dont-vary
Thanks so much!
Just posting the result as an answer so I can mark the question as answered. Thanks to all for the helpful comments!
I found a forum on the hosting provider’s website where someone explained that gzip has recently been turned on, and detailed how to stop it. http://www.bluehostforum.com/showthread.php?18996-Turning-off-Gzip
Basically, I added the following to my .htaccess file, which turns off the gzip compression. Then, I get the Content-Length header, and my progress bar.