Yehuda
Yehuda

Reputation: 1382

Download file from external website with InAppBrowser - Cordova

I'm working on android app and I use Cordova and AngularJS...

I want to open external link with InAppBrowser and allow the user to download file from a link inside this site...

When I click the button that should open the InAppBrowser it's opens the website but when I click on download link inside this site, nothing really happens...

The app have

 "android.permission.WRITE_EXTERNAL_STORAGE"

permission

Thanks :)

Upvotes: 4

Views: 5463

Answers (1)

AtanuCSE
AtanuCSE

Reputation: 8940

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);
                    }
                });

add this two import import android.net.Uri; import android.webkit.DownloadListener;

Don't know about iOS

Upvotes: 4

Related Questions