Avoiding content type issues when downloading a file via browser on Android
To make any downloads work on all (and especially older) Android versions as expected, you need to...
- set the ContentType to application/octet-stream
- put the Content-Disposition filename value in double quotes
- write the Content-Disposition filename extension in UPPERCASE
Read my blog post for more details:
http://digiblog.de/2011/04/19/android-and-the-download-file-headers/
Android is sending 2 requests to download one file via mobile browser
I am in a dead end :(
http://code.google.com/p/android/issues/detail?id=1978
The browser needs to hit the server to determine that something is a download, and than the download manager has to separately contact the server for downloading.
So, Android fire up first request to open dialogue box for "are you sure? - yes/no".
And if user click "Yes" Android Forward request to Android DownloadManager and manager send second request to download file. (some Android version send request to DownloadManager immediately)
The problem is that both request, first and second, are type of GET (not HEAD).
Unable to download file with content type text/html
In your case it looks like url is redirected into another URL from which real content is downloaded.
You need to get check the Location
header and if its non null then get value from header close connection and open new one on that link.
Then when you invoke method getContentType() it will be application/pdf
Do I need Content-Type: application/octet-stream for file download?
No.
The content-type should be whatever it is known to be, if you know it. application/octet-stream
is defined as "arbitrary binary data" in RFC 2046, and there's a definite overlap here of it being appropriate for entities whose sole intended purpose is to be saved to disk, and from that point on be outside of anything "webby". Or to look at it from another direction; the only thing one can safely do with application/octet-stream is to save it to file and hope someone else knows what it's for.
You can combine the use of Content-Disposition
with other content-types, such as image/png
or even text/html
to indicate you want saving rather than display. It used to be the case that some browsers would ignore it in the case of text/html
but I think this was some long time ago at this point (and I'm going to bed soon so I'm not going to start testing a whole bunch of browsers right now; maybe later).
RFC 2616 also mentions the possibility of extension tokens, and these days most browsers recognise inline
to mean you do want the entity displayed if possible (that is, if it's a type the browser knows how to display, otherwise it's got no choice in the matter). This is of course the default behaviour anyway, but it means that you can include the filename
part of the header, which browsers will use (perhaps with some adjustment so file-extensions match local system norms for the content-type in question, perhaps not) as the suggestion if the user tries to save.
Hence:
Content-Type: application/octet-stream
Content-Disposition: attachment; filename="picture.png"
Means "I don't know what the hell this is. Please save it as a file, preferably named picture.png".
Content-Type: image/png
Content-Disposition: attachment; filename="picture.png"
Means "This is a PNG image. Please save it as a file, preferably named picture.png".
Content-Type: image/png
Content-Disposition: inline; filename="picture.png"
Means "This is a PNG image. Please display it unless you don't know how to display PNG images. Otherwise, or if the user chooses to save it, we recommend the name picture.png for the file you save it as".
Of those browsers that recognise inline
some would always use it, while others would use it if the user had selected "save link as" but not if they'd selected "save" while viewing (or at least IE used to be like that, it may have changed some years ago).
Not able to download files with php in android browsers
I got the solution.
Mainly I changed two things:
- Used GET method instead of Post Method.
- Used Flush and ob_clean functions to clear the buffer.
New code for downfile.php is like this:
<?php
require '../php/db.php';
ob_start();
if(isset($_GET['file_id'])&&!empty($_GET['file_id'])){
download_file($_GET['file_id']);
}else die("There was an error in downloading file. Please try again later.");
function download_file($id){
global $con;
$id = mysqli_real_escape_string($con,htmlentities($id));
$file="SELECT file_name,file_title,file_size,down FROM files WHERE file_id= $id";
$result = mysqli_query($con,$file);
$row = mysqli_fetch_assoc($result);
$name = $row['file_name'];
$title = $row['file_title'];
$ext = ext($name);
$down = $row['down'];
$newname = $title.'.'.$ext;
$size = $row['file_size'];
$down++;
if(is_file($name)) {
$update_down = "UPDATE files SET down = $down WHERE file_id = '$id'";
$update_down_result = mysqli_query($con,$update_down);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$newname.'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: '.$size);
ob_clean();
flush();
readfile($name);
exit;
}else header("Location: index.php?msg=Sorry!+File+could+not+found!");
}
function ext($name){
$rut = strrev($name);
$erut = explode('.', $rut);
return strrev($erut[0]);
}
?>
Through this code I am able to download files in android browsers too.
Hope this may help. :)
Download file in java when Content-Length is incorrectly set?
After some research I concluded that my use of
BufferedInputStream
to append to aByteArrayBuffer
is autosizing the byte array to the url connections content length.
Nonsense. You are crediting those classes with paranormal powers. How could an output stream possibly become aware of the Content-length header? The URLConnection
's input stream is being terminated at the content-length. Correctly.
To circumvent this, I tried to use ByteArrayOutputStream instead, however this solved nothing.
Of course not.
Anybody know of a way to download a file if the Content-Length is incorrectly set?
You could use a Socket
and engage in HTTP yourself, which is less trivial than it sounds. But the problem is at the server and that's where it should be fixed. Complain. Or else @Zong Yu is correct and the page is HTML containing JavaScript, say.
NB You don't need to read the entire file into memory:
while((newLength = inputStream.read(buffer))>0)
{
curLength += newLength;
fos.write(buffer, 0, newLength);
}
Related Topics
Android Button Has Setontouchlistener Called on It But Does Not Override Performclick
Genymotion Unable to Start the Genymotion Virtual Device
Android Java - Joda Date Is Slow
Fcm (Firebase Cloud Messaging) Push Notification with ASP.NET
How to Start an Android Application from the Command Line
How to Capture and Save an Image Using Custom Camera in Android
Android - Swipe to Delete Recyclerview
Android: How to Convert Whole Imageview to Bitmap
Libgdx Spritebatch Render to Texture
How to Create Converter for My Class in Android Retrofit Library
How to Add Bulleted List to Android Application
Android How to Programmatically Hide Launcher Icon
How to Avoid Delay in Android Gcm Messages/Change Heartbeat
(Deprecated) Fragment Onoptionsitemselected Not Being Called
App Always Starts Fresh from Root Activity Instead of Resuming Background State (Known Bug)