2

I am stuck trying to download an XML file by pressing a button which first generates XML and then downloads file in browser such as chrome etc. I have to render this web page in an in-app browser. I am using web view/xamarin forms which is almost serving the purpose i.e. I can interact and make adjustments to other buttons on the web page but when I click the download button it does nothing. I have searched a lot and the closest question I have come across is here;

https://forums.xamarin.com/discussion/96237/file-download-via-xamarin-forms-webview

still this is not what I want I have posted this question on the link above too but no response. Also the question above downloads using a downloadable link and does this using browser like chrome etc. On the other hand I don't want to leave in-app browser to download file. I have read its documentation here;

https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/webview?tabs=vswin#Events

but it says nothing about downloading a file nor it says anything about any alternatives to it. I don't have much code to show, this is what I am doing at XAML side

<StackLayout>
   <WebView x:Name="webView" HeightRequest="1000" WidthRequest="1000"
            Source="http://example.com/b/" VerticalOptions="Center"
            AbsoluteLayout.LayoutBounds="0.95, 0.05, 100, 50" 
            AbsoluteLayout.LayoutFlags="All" />
</StackLayout>

Any help is really appreciated, I have seen other questions but most of them talk about downloading from a URL which I don't have or old Nuget packages.

Ammar Bayg
  • 105
  • 10
  • Are you doing anything with the data besides displaying it? If not, do you really need to download a local copy? Why not just pass the files URL to the browser control and let it display? – Jason Apr 02 '18 at 17:51
  • If the Download button is within the HTML/JavaScript code, use JavaScript to "download" the XML file via XMLHttpRequest into a JavaScript variable and process it in JavaScript. – SushiHangover Apr 02 '18 at 17:53
  • @Jason I really need to download a local XML copy, actually I am not displaying the data I am trying to download, I am trying to download some settings which are result of my interaction with other buttons on the same web page. When I press download button it generates some XML which include that settings and then download that file. – Ammar Bayg Apr 02 '18 at 17:59
  • @SushiHangover download button is handled from react. I don't have that code. No URL changes, no request in network tab dev tools chrome. Also download button first creates XML and then downloads that XML. – Ammar Bayg Apr 02 '18 at 18:01
  • For Android, the SetDownloadListener doesn't serve the purpose? – Hichame Yessou Apr 02 '18 at 19:08
  • @hichame.yessou as far as I know xamarin forms doesn't have this method, have a look at the accepted answer and a comment below https://stackoverflow.com/questions/10069050/download-file-inside-webview. It says this reloads link in browser that is not what I want. – Ammar Bayg Apr 02 '18 at 19:13
  • WebView will fire an event when you navigate to a new page - if that is how the download link is implemented then you can easily intercept it. But without knowing something about how the download logic in the page is written, it's hard to answer your question. – Jason Apr 02 '18 at 19:19
  • Not Xamarin.Forms but an Android custom renderer, as in the first link of your question. It doesn't reload the link in an external browser – Hichame Yessou Apr 02 '18 at 19:21
  • @Jason download is handled through a react function which I don't have code for, but there is no request in network tab of dev tools chrome, neither URL changes after download or when download button is clicked. – Ammar Bayg Apr 02 '18 at 19:25
  • You can't solve this problem until you figure that out. Even without the source you should be able to inspect the JS in the console and determine what happens when you click that button. – Jason Apr 02 '18 at 19:29
  • @Jason I got that, I will let you know when once I would figure that out. – Ammar Bayg Apr 02 '18 at 19:31
  • @Jason I have inspected it. First it is creating a blob object, then creating a URL using js `createObjectURL(blobObject)` , then it is creating a temporary anchor tag and setting its href to blob URL and setting its download attribute. – Ammar Bayg Apr 02 '18 at 20:31

0 Answers0