简体   繁体   中英

APK download doesn't work on stock Android browser

I have a page with a button that redirects to a PHP script that starts an APK download, like so ($androidPackage is the file name and $package contains the absolute path):

   header('Content-Description: File Transfer');
   header('Content-Type: application/vnd.android.package-archive');
   header('Content-Disposition: attachment; filename=' . $androidPackage);
   header('Content-Transfer-Encoding: binary');
   header('Expires: 0');
   header('Cache-Control: must-revalidate');
   header('Pragma: public');
   header('Content-Length: ' . filesize($package));
   ob_clean();
   flush();
   readfile($package);
   exit; 

This approach works on Chrome for several Android devices, all of the major desktop browsers, and on the stock browser on some Android devices. However, on my phone (HTC Rezound) and the Galaxy Tab 2, on the stock browser, the download does not work on a short press of the button - it doesn't start, even though this works on Chrome on those devices. A long press on the button, followed by selection of 'Open' from the context menu that appears, does work to download the APK from the stock browser. Any ideas as to why the download doesn't start with a short press on the stock browser?

edit:

I should mention that the button used to link to this download is located in an iframe. If instead I do a document.location.href = (download script URL) instead of loading the script's source into the iframe when the document loads, the download does work with a short press. Something to do with how the stock browser handles iframes? The page and the script it points to are on the same domain.

将您的内容类型更改为octet-stream

header('Content-Type: application/octet-stream');

The issue was that it's in an iframe. adding

target="_blank"

to the anchor tag pointing to the download, per This question's answer , fixed me right up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM