I am performing a POST to the Android webview where the expected response is a PDF file. However the webview just shows a blank page. I realise that PDF files cannot be rendered in the webview but would expect the file to start downloading or show some response at least.
Does anyone know if its possible to POST to a webview to initiate download of a (pdf) file?
_webView = FindViewById<WebView>(Resource.Id.ebookWebview);
_webView.SetWebViewClient(webviewClient);
_webView.SetWebChromeClient(new WebChromeClient());
_webView.Settings.AllowFileAccess = true;
_webView.Settings.JavaScriptEnabled = true;
_webView.PostUrl(_postUrl, EncodingUtils.GetBytes(_postData, "BASE64"));
Have you tried registering a DownloadListener on the WebView? That is needed to handle the download. Please see http://developer.android.com/reference/android/webkit/WebView.html#setDownloadListener(android.webkit.DownloadListener)
Related
I have a webview that shows a website which i did not make so i dont know the details of how it works.
On this site there are several buttons which download various files which are generated on the fly. Here is an example of the url request used on one of these buttons: test.example.com/Test/Export/Stuff?format=Pdf
This causes a file to be downloaded on my desktop browser and on my phones chrome but nothing happens on my app.
I have scoured the internet searching for a solution but i am unable to find one that works.
I have tried setting DownloadListener as discribed here: https://forums.xamarin.com/discussion/4605/download-file-by-webview but the OnDownloadStart never triggers.
I have also tried intercepting the url request using ShouldOverrideUrlLoading in my custom WebViewClient as descibed in other posts with no luck.
Here is the html code for the button:
<input id="exportPdfButton" class="secondary hover" format="Pdf" value="Download (PDF)" name="exportPdfButton" type="submit">
<script id="dxss_848651839" type="text/javascript">
<!--
var dxo = new MVCxClientButton('exportPdfButton');
dxo.InitGlobalVariable('exportPdfButton');
dxo.SetProperties({'autoPostBack':true,'isNative':true});
dxo.SetEvents({'Click':ExportButtonOnClick});
dxo.AfterCreate();
//-->
</script>
I have set permissions for ACCESS_DOWNLOAD_MANAGER, WRITE_EXTERNAL_STORAGE etc.
Can anyone help me figure out how i can download these files in the app? Otherwise is there any other information i can provide to help?
Can anyone help me figure out how i can download these files in the app?
Firstly, please make sure your WebView has enabled javascript and the WebViewClient is set correctly:
mWebview = FindViewById<WebView>(Resource.Id.mWebview);
mWebview.Download += MWebview_Download;
var client = new WebViewClient();
mWebview.Settings.JavaScriptEnabled = true;
mWebview.SetWebViewClient(client);
mWebview.LoadUrl("your url");
Then in the WebView.Download event use DownloadManager to download the file:
private void MWebview_Download(object sender, DownloadEventArgs e)
{
var url = e.Url;
DownloadManager.Request request = new DownloadManager.Request(Uri.Parse(url));
request.AllowScanningByMediaScanner();
request.SetNotificationVisibility(DownloadManager.Request.VisibilityVisibleNotifyCompleted); //Notify client once download is completed!
request.SetDestinationInExternalPublicDir(Environment.DirectoryDownloads, "CPPPrimer");
DownloadManager dm = (DownloadManager)GetSystemService("download");
dm.Enqueue(request);
Toast.MakeText(ApplicationContext, "Downloading File",ToastLength.Long//To notify the Client that the file is being downloaded
).Show();
}
I know this question has been asked so many time , but i didn't get any proper and satisfactory solution. I want to show the Pdf file in web view , which is coming from the url ,when i click on the URL , my pdf downloads. Currently i am usinghtml following code for showing pdf file in web view , but it is opening the html file. I am getting stuck in this since 4 hours.Kindly suggest me where i am doing wrong.
Thanks in advance.
mWebview.getSettings().setJavaScriptEnabled(true);
WebSettings settings = mWebview.getSettings();
settings.setDefaultTextEncodingName("utf-8");
mWebview.loadData(response, "application/pdf; charset=utf-8",null);
i have following response coming in following format :-
**%PDF-1.4%���5 0 obj<</E 74937/H [ 1453 173 ]/L 75330/Linearized 1/N 1/O 8/T 75180>>**
I want to show the Pdf file in web view
WebView does not have a PDF renderer.
Kindly suggest me where i am doing wrong.
You are attempting to load a PDF directly into a WebView. Use something else to render the PDF.
For show pdf in webview use Direct url:
https://docs.google.com/gview?embedded=true&url="PDF_FILE_URL"
Now load above url in webview.
mWebview.loadUrl("https://docs.google.com/gview?embedded=true&url="PDF_FILE_URL"");
I have a problem with google doc viewer for display xls file get from my server.
Currently when i try to display my xls file get from my server in my android app with a webview, i have a grey page with no preview available
(I can download the xls from my server with the url).
Sorry, I could not share link but It's like xxxx.com/123/DownloadExcel_GET?paraOne=%s¶Two=%s¶Three=%s.
But when i try with xls exemple file in internet like http://lecompagnon.info/demos/demoxl3.xls , i don't have this problem. The xls is display succesfully.
Same it's working if i try with a googleDrive url like "drive.google.com/file/t/myfile/view?usp=sharing".
The only difference with the exemple file in internet and my file from my server, is the time to start the download.
With the exemple file is instant, but with my xls i have to wait more than 10sec to start the download.
I think google doc viewer return no preview available, because for him 10sec to get the file is too long and he return a time out. But it's a supposition...
I found nothing about this problem, only this topic Google Document Viewer shows "No Preview Available" .
Here is my code :
String urlgoogle = "https://docs.google.com/gview?embedded=true&url=";
String completeURL = urlgoogle + query;
Log.i("completeURL", completeURL);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(completeURL);
//wv.loadUrl("https://docs.google.com/gview?embedded=true&url=" + "http://lecompagnon.info/demos/demoxl3.xls"); working
wv.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
edit
I add more information.
The grey page with no preview available not always showing, sometimes my xls file is display successfully the first time.
If i have the grey page, i can refresh, and after one or two refresh, the file is display. The refresh just call again the url in the webview
The file is dynamically create and generated from the serveur, this is why for download or get the file i must wait 10 sec.
Do Google doc viewer have a timeout if the time to get the file to display is too long ?
Basically, I have a Webview that points to an internal "note.html" file.
In this note.html file, which is written in HTML, I have a website HREF, but when I click on it on a device, I get an error message:
"the webpage at file:///android/asset/www.blahblah.com might be temporarily down ...etc."
I am somewhat aware that I have to parse the URL or something of that nature, and would really appreciate a response on how to get the HREF link to open the proper web link to a browser.
the variable:
private static final String tip_URL = "file:///android_asset/tip.html";
the onCreate:
WebView mywebview = (WebView) findViewById(R.id.webview); mywebview.loadUrl(tip_URL);
this works fine on a mobile device, however, in the tip.html there is a link to a website, and when clicked i want a browser to open to go to that site..it is not another local page..
I have a requirement where there is a URL = "http://www.example/Open.pdf"
Now from my android application I want to open this PDF file directly in the default PDF viewer.
The moment I click on this link on the webpage, user should be presented with a default PDF viewer opened with this document.
Note: This file should not be stored on the SD card.
How do I proceed for this implementation?
We can open PDF file in the webview without caching it. Write below code in "onCreate" method .
Working code :
String url = "http://www.example.com/abc.pdf";
final String googleDocsUrl = "http://docs.google.com/viewer?url=";
WebView mWebView=new WebView(this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return false; // then it is not handled by default action
}
});
mWebView.loadUrl((googleDocsUrl + url));
setContentView(mWebView);
What happens here is you open the PDF using Google Docs. Best Advantage of using above method is the lazy loading of PDF. Does not matter how heavy the PDF is. Google Docs takes care of it.
You can view the pdf in the WebView using googleDocs.
WebView webView = (WebView) findViewById(R.id.my_webview);
webView.setWebViewClient(new MyWebViewClient());
webView.addView(webView.getZoomControls());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf");
There is no way you can open a default PDF view from your application.
If your file is on the server and you want to open it without downloading then this might also pose a greater security concern. If external applications like default adobe reader can access the content on your server, then this is breaking the security framework altogether.
So, best option would be to launch a new instance of browser or webview and show the PDF document in google docs to the user.
This way user can read the document and get back to the recent state of the application as well.
You can view the pdf in the WebView using googleDocs.
WebView webView = (WebView) findViewById(R.id.my_webview);
webView.setWebViewClient(new MyWebViewClient());
webView.addView(webView.getZoomControls());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf");
do you have the others solution besides view pdf file using http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf