Download from moodle with download manger
We area creating Xamarin Application, this app open moodle web site in native web view, we handle the action with Download event everything go well, get the link to download and process it to downloaded manager, but here is the problem Moodle reject the request to download the file, the method download correctly other files who doesn't are inside Moodle (any other server).
I try with different methods, DownloadManager and HTTP request directly but doesn't work.
Here is my download method:
private void MWebview_Download(object sender, DownloadEventArgs e)
{
string fileName = System.IO.Path.GetFileName(e.Url);
try
{
// Initialize and set configurations to DownloadManager
DownloadManager.Request request = new DownloadManager.Request(Uri.Parse(e.Url));
var map = MimeTypeMap.Singleton;
string extension = MimeTypeMap.GetFileExtensionFromUrl(e.Url);
string mimeType = map.GetMimeTypeFromExtension(extension);
request.SetMimeType(mimeType);
request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted); //Notify client once download is completed!
request.SetDestinationInExternalPublicDir(Environment.DirectoryDownloads, fileName);
request.SetTitle(fileName);// Title of the Download Notification
request.SetDescription("Downloading");// Description of the Download Notification
request.SetAllowedNetworkTypes(DownloadNetwork.Mobile | DownloadNetwork.Wifi);
request.SetRequiresCharging(false);// Set if charging is required to begin the download
request.SetAllowedOverMetered(true);// Set if download is allowed on Mobile network
request.SetAllowedOverRoaming(true);// Set if download is allowed on roaming network
//Get ans set cookies
string cookies = CookieManager.Instance.GetCookie("https://www.example.com/moodle3/my/");
string[] AllCookies = cookies.Split(";");
foreach (var item in AllCookies)
{
string[] temp = item.Split("=");
request.AddRequestHeader(temp[0], temp[1]);
}
if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) == (int)Permission.Granted)
{
DownloadManager dm = (DownloadManager)GetSystemService(DownloadService);
dm.Enqueue(request);
Toast.MakeText(ApplicationContext, "File Downloaded", ToastLength.Long//To notify the Client that the file is being downloaded
).Show();
}
else
{
// Storage permission is not granted. If necessary display rationale & request
Permissions.RequestAsync<Permissions.StorageWrite>();
if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) == (int)Permission.Granted)
{
// We have permission, go ahead
DownloadManager dm = (DownloadManager)GetSystemService(DownloadService);
dm.Enqueue(request);
Toast.MakeText(ApplicationContext, "File Downloaded", ToastLength.Long//To notify the Client that the file is being downloaded
).Show();
}
else
{
Toast.MakeText(ApplicationContext, "File not downloaded", ToastLength.Long//To notify the Client that the file is being downloaded
).Show();
}
}
}
catch (Exception )
{
Toast.MakeText(ApplicationContext, "Fail Download File", ToastLength.Long//To notify the Client that the file is being downloaded
).Show();
}
}
}
Important, in the native web view already log in.