[英]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.