PhoneGap / Cordova InAppbrowser文件下载问题 [英] Phonegap/Cordova InAppbrowser File Download issue

查看:515
本文介绍了PhoneGap / Cordova InAppbrowser文件下载问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PhoneGap / Cordova开发移动应用程序。

我使用InAppBrowser在Cordova中使用window.open方法显示外部网站。



外部网站显示一些文件列表和下载链接。当我点击下载超链接时,它在所有浏览器中正常工作。



但是当我从Cordova Mobile应用程序中的InAppBrowser调用该外部网站时,



当我们使用InAppBrowsers时,我们无法使用Cordova API。如何解决此问题?



更新:



ASP.Net中的响应网站。这个网站的功能之一是将文件以字节形式保存到SQL服务器,并在需要时作为文件下载。



我有一个GridView控件来显示列表文件和选项下载。
当您单击要下载的文件时,服务器端代码将从SQL Server创建文件并使用HTTP响应附加它。



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

在桌面浏览器中运行此Web应用程序时,它将按预期工作。 。



我已将此网站包装在Phonegap / Cordova Mobile应用程式中。



Javascript:

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

网站将在InAppBrowser上运行。它无法处理下载文件http响应。



我们可以通过修改android / IOS本机代码来克服这个问题。
我没有访问android / IOS代码。因为我使用IntelXDK在云中构建android / IOS应用程序。



下面提供了整体问题。
in Cordova InAppBrowser,如何处理包含文件内容类型的Http响应?

解决方案

InAppBrowser不允许下载。



对于android, platforms \android \src\org\apache \cordova\inappbrowser



方法名称 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);
}
});

之前 this.inAppWebView.requestFocus();



方法中的相同代码 public void run(){



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

inAppWebView.loadUrl(url); onCreate中的.java文件中的




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

不知道iOS


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

这篇关于PhoneGap / Cordova InAppbrowser文件下载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆