I am trying to download some files from my MAUI application. Application is built on a webView from where files should be downloaded. I have this logic in my DownloadListner:
public class CustomDownloadListener : Java.Lang.Object, IDownloadListener
{
public async void OnDownloadStart(string url, string userAgent, string contentDisposition, string mimetype, long contentLength)
{
try
{
DownloadManager.Request request = new DownloadManager.Request(global::Android.Net.Uri.Parse(url.Replace("blob:", "")));
request.SetMimeType(mimetype);
request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
// if this path is not create, we can create it.
string thmblibrary = FileSystem.AppDataDirectory + "/download";
if (!Directory.Exists(thmblibrary))
Directory.CreateDirectory(thmblibrary);
request.SetDestinationInExternalFilesDir(global::Android.App.Application.Context, FileSystem.AppDataDirectory, "download");
DownloadManager dm = (DownloadManager)global::Android.App.Application.Context.GetSystemService(global::Android.App.Application.DownloadService);
dm.Enqueue(request);
}
catch(Exeption ex)
{
}
}
}
this works well for transferring most of the files in the app, but if the URL starts with the "blob:https://..." DownloadManager will throw an exception. I also tried to remove "blob:" from the URL in this case, the file gets downloaded but it is corrupted.
Is there any other way to transfer those files?
This is the limit in the android webview.
Android DownloadManager is designated for downloading remote files using HTTP / HTTPS protocols only. A blob is not a remote data, but something within your WebView. So the workaround is to convert the Blob into Base64 string and write it to a file.
You can refer to the case about Unable to download Blob file type in WebView on Android and the case about Download Blob file from Website inside Android WebViewClient.
In addition, you can refer to this case about how to use JavaScriptInterface in the xamarin.android and this answer about using JavaScriptInterface in the maui.