Daddoon / BlazorMobile

Create full C# driven hybrid-apps for iOS, Android, UWP & Desktop with Blazor!
MIT License
413 stars 55 forks source link

[question] File download handling over mobile web apps. #220

Closed arivera12 closed 4 years ago

arivera12 commented 4 years ago

If a trigger a file download via JavaScript,

How does mobile web app handle this?

How does mobile web app handles a file download via content-disposition attachment header?

Do I will need to create a function which manage this and write to disk using the IXamarinBridge or this will auto trigger the save file dialog?

Daddoon commented 4 years ago

As Blazor is a SPA application, except if you do some custom implementation to mimic or handle a web server callback for the app, the easiest way in my opinion, not specially the fastest in term of performance, is to try to retrieve the selected file through JavaScript API after file selection, and then do whatever you want: Return this to Web C# context, or show it on the page directly if you need to, or transfer it to native.

With the JavaScript API you will get the data as text ideally (Base64) then send it to C# , then use a Proxy bridge to send the content to native C#.

Bytes support may be supported at Blazor level, but not sure, same for the interop to native. So a Base64 string is still the safest solution atm.

Daddoon commented 4 years ago

Some hint here for the low level thing on the browser : https://stackoverflow.com/questions/36280818/how-to-convert-file-to-base64-in-javascript

And

https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL

arivera12 commented 4 years ago

I have a package for that. https://github.com/arivera12/BlazorDownloadFile

I will give it a try once I can run the project on android I will give feedback for this.

I wanna see how this behave on mobile device installed app if save file dialog gets popped out or if I need xamarin bridge to use the c# file write method.

arivera12 commented 4 years ago

This won't work on BlazorMobile since it's sanboxed by the browser control.

We need to write directly to the device disk.

We need to separate those apis one for the browser and one for native xamarin.

Daddoon commented 4 years ago

As you can also fetch your data through a file input in the web browser and redirect it to Javascript, you may use the default file input in order to retrieve data and then store it to native definitively.

But yes, you may redirect to a native call that open a File browser when clicking on a button.

It's as you wish in the end. I didn't have benchmarked the performance while using a pure web implementation if you need to redirect the data to store to native.