[英]Phonegap/Cordova InAppbrowser File Download issue
我正在使用 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 移动应用程序中。
Javascript:
window.open(encodeURI("http://example.com/Files.aspx"), '_blank', 'location=no');
该网站将在 InAppBrowser 上运行。 它无法处理下载文件 http 响应。
我们可以通过修改 android/IOS 原生代码来解决这个问题。 我无法访问 android/IOS 代码。因为我使用 IntelXDK 在云中构建 android/IOS 应用程序。
以下提供了总体问题。 在 Cordova InAppBrowser 中,如何处理包含文件内容类型的 Http 响应?
InAppBrowser 不允许下载。 您需要修改插件以允许下载。
对于android,在platforms\\android\\src\\org\\apache\\cordova\\inappbrowser
方法名称private void navigate(String url) {
包括
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 文件中
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.