3

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?

CJM
  • 249
  • 1
  • 4
  • 16
  • Seems there are a problem with downloads when you do a POST to init the download. I have the same problem. The Downloadmanager only seems to work when you call the download with a GET. – David Aug 31 '17 at 08:43

1 Answers1

2

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();
    }
Elvis Xia - MSFT
  • 10,801
  • 1
  • 13
  • 24
  • Yes i already had javascript enabled and have the client set. I have tried the .Download Event and it just doesnt trigger when the button is clicked. – CJM May 03 '17 at 13:02
  • Then it is most likely the js scripts' problem. Could you please post the js function of `Download(PDF)`? – Elvis Xia - MSFT May 04 '17 at 01:36
  • It doesnt appear that it uses javascript, I must admit i am very much a novice at JavaScript. But from what i can see it uses a Post in the containing form.
    – CJM May 04 '17 at 07:59
  • That's exactly where the problem is, webview doesn't let you access the content of http response. Please refer to [this case](http://stackoverflow.com/questions/13452289/how-to-make-post-requests-with-webview). So instead of using webview, you should use `HttpClient`. – Elvis Xia - MSFT May 04 '17 at 08:13
  • 1
    I have tried with the above solution. It returns corrupted PDF file but I am not able to open the file. It shows an error message "Can't open PDF". I have PDF reader installed on my phone. – Alpesh Dec 15 '18 at 18:59
  • Hi Alpesh, same problem occurring with me. can you please guide me ? – Vishal Khunt Sep 26 '19 at 13:05