Download file from external website with InAppBrowser - Cordova

落花浮王杯 提交于 2021-02-07 19:52:39

问题


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 :)


回答1:


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



来源:https://stackoverflow.com/questions/30390826/download-file-from-external-website-with-inappbrowser-cordova

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!