简体   繁体   中英

Phonegap/Cordova InAppbrowser File Download issue

I'm developing a mobile app using PhoneGap/Cordova.
I use InAppBrowser to show an external website using "window.open" method in Cordova.

The external website displays some files list and download links. When I click the download hyperlinks, it works fine in all browsers.

But when I call that external website from InAppBrowser in the Cordova Mobile app, the download links in that page are not functional.

We can't use the Cordova API when we are using InAppBrowsers. How can I overcome this issue?

UPDATE :

I've Developed a Responsive website in ASP.Net. One of the functionality of this website is to save files to SQL server as bytes and download as file when it is needed.

I have a GridView control to display the list of files and options to download. When you click a file to download, Server side code will create file from the SQL Server and attached it with HTTP Response.

C# code:

        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = contentType;
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();

When you run this web application in the desktop browsers, It will work as expectedly.File will be downloaded.

I've wrapped this website in a Phonegap/Cordova Mobile app.

Javascript:

window.open(encodeURI("http://example.com/Files.aspx"), '_blank', 'location=no');

The website will be running on InAppBrowser. It could not handle the download file http response.

We can overcome this issue by modifying the android/IOS native codes. I have no access to android/IOS code.Because I use IntelXDK to Build the android/IOS app in the cloud.

Following provides the Overall Issue. in Cordova InAppBrowser, How to handle the Http response that contains file content types?

InAppBrowser doesn't allow download. You will need to modify plugin to allow it for downloading.

For android, inside platforms\\android\\src\\org\\apache\\cordova\\inappbrowser

method name private void navigate(String url) {

include

this.inAppWebView.setDownloadListener(new DownloadListener() {
                    public void onDownloadStart(String url, String userAgent,
                            String contentDisposition, String mimetype,
                            long contentLength) {
                      Intent i = new Intent(Intent.ACTION_VIEW);
                      i.setData(Uri.parse(url));
                      cordova.getActivity().startActivity(i);
                    }
                });

before this line this.inAppWebView.requestFocus();

again same code in the method public void run() {

after this segment

if (clearAllCache) {
                CookieManager.getInstance().removeAllCookie();
            } else if (clearSessionCache) {
                CookieManager.getInstance().removeSessionCookie();
            }

            inAppWebView.loadUrl(url);

in your .java file inside onCreate

appView.setDownloadListener(new DownloadListener() {
                    public void onDownloadStart(String url, String userAgent,
                            String contentDisposition, String mimetype,
                            long contentLength) {
                      Intent i = new Intent(Intent.ACTION_VIEW);
                      i.setData(Uri.parse(url));
                      startActivity(i);
                    }
                });

Don't know about iOS

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