简体   繁体   English

Phonegap/Cordova InAppbrowser 文件下载问题

[英]Phonegap/Cordova InAppbrowser File Download issue

I'm developing a mobile app using PhoneGap/Cordova.我正在使用 PhoneGap/Cordova 开发移动应用程序。
I use InAppBrowser to show an external website using "window.open" method in Cordova.我使用 InAppBrowser 在 Cordova 中使用“window.open”方法显示外部网站。

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.但是,当我从 Cordova Mobile 应用程序中的 InAppBrowser 调用该外部网站时,该页面中的下载链接不起作用。

We can't use the Cordova API when we are using InAppBrowsers.当我们使用 InAppBrowsers 时,我们不能使用 Cordova API。 How can I overcome this issue?我怎样才能克服这个问题?

UPDATE :更新 :

I've Developed a Responsive website in ASP.Net.我在 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.该网站的功能之一是将文件以字节形式保存到 SQL 服务器,并在需要时以文件形式下载。

I have a GridView control to display the list of files and options to download.我有一个 GridView 控件来显示要下载的文件和选项列表。 When you click a file to download, Server side code will create file from the SQL Server and attached it with HTTP Response.当您单击要下载的文件时,服务器端代码将从 SQL Server 创建文件并将其附加到 HTTP 响应中。

C# code: C#代码:

        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.当您在桌面浏览器中运行此 Web 应用程序时,它将按预期工作。文件将被下载。

I've wrapped this website in a Phonegap/Cordova Mobile app.我已经将这个网站包装在一个 Phonegap/Cordova 移动应用程序中。

Javascript: Javascript:

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

The website will be running on InAppBrowser.该网站将在 InAppBrowser 上运行。 It could not handle the download file http response.它无法处理下载文件 http 响应。

We can overcome this issue by modifying the android/IOS native codes.我们可以通过修改 android/IOS 原生代码来解决这个问题。 I have no access to android/IOS code.Because I use IntelXDK to Build the android/IOS app in the cloud.我无法访问 android/IOS 代码。因为我使用 IntelXDK 在云中构建 android/IOS 应用程序。

Following provides the Overall Issue.以下提供了总体问题。 in Cordova InAppBrowser, How to handle the Http response that contains file content types?在 Cordova InAppBrowser 中,如何处理包含文件内容类型的 Http 响应?

InAppBrowser doesn't allow download. InAppBrowser 不允许下载。 You will need to modify plugin to allow it for downloading.您需要修改插件以允许下载。

For android, inside platforms\\android\\src\\org\\apache\\cordova\\inappbrowser对于android,在platforms\\android\\src\\org\\apache\\cordova\\inappbrowser

method name private void navigate(String url) {方法名称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();在这行之前this.inAppWebView.requestFocus();

again same code in the method public void run() {方法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在 onCreate 内的 .java 文件中

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不知道iOS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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