How to get list of downloaded files from External File Directory in xamarin.forms?

Multi tool use
Multi tool use


How to get list of downloaded files from External File Directory in xamarin.forms?



I am working in xamarin.forms. I want to download file and also want to show all the downloaded files as a list. I want to implement this feature in both android and IOS.



I took reference from below link to download files. It was successfully implemented.



https://forums.xamarin.com/discussion/71203/how-to-download-a-file-in-xamarin-forms-and-store-it-in-the-device-storage



But Now I want to list out all the downloaded files which are available in that folder. I did not any solution for the same.



For android :


public void Downloaded()
{
CrossDownloadManager.Current.PathNameForDownloadedFile = new Func<Plugin.DownloadManager.Abstractions.IDownloadFile, string>(file => {
string fileName = Android.Net.Uri.Parse(file.Url).Path.Split('/').Last();
return Path.Combine(ApplicationContext.GetExternalFilesDir(Android.OS.Environment.DirectoryDownloads).AbsolutePath,fileName);

});
}



For IOS :


public void Downloaded()
{
CrossDownloadManager.Current.PathNameForDownloadedFile = new Func<Plugin.DownloadManager.Abstractions.IDownloadFile, string>(file => {
string fileName = (new NSUrl(file.Url, false)).LastPathComponent;
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), fileName);

});
}



These both function I have called in MainActivity and AppDelegate respectively.



IN PCL Content Page :


public partial class MainPage : ContentPage
{
public bool isDownloading = true;
public IDownloadFile File;
public MainPage()
{
InitializeComponent();
CrossDownloadManager.Current.CollectionChanged += (sender, e) => System.Diagnostics.Debug.WriteLine(
"[DownloadManager] " + e.Action +
" -> New items: " + (e.NewItems?.Count ?? 0) +
" at " + e.NewStartingIndex +
" || Old items: " + (e.OldItems?.Count ?? 0) +
" at " + e.OldStartingIndex
);
}

public async void DownloadFile(string FileName)
{
await Task.Yield();
await Task.Run(() =>
{
var downloadManager = CrossDownloadManager.Current;
var file = downloadManager.CreateDownloadFile(FileName);
downloadManager.Start(file, true);
while(isDownloading)
{
isDownloading = IsDownloading(file);
}
});
if(!isDownloading)
{
await DisplayAlert("Success", "Your file has been downloaded", "Ok");
}
}

public bool IsDownloading(IDownloadFile File)
{
if (File == null) return false;

switch (File.Status)
{
case DownloadFileStatus.INITIALIZED:
case DownloadFileStatus.PAUSED:
case DownloadFileStatus.PENDING:
case DownloadFileStatus.RUNNING:
return true;

case DownloadFileStatus.COMPLETED:
case DownloadFileStatus.CANCELED:
case DownloadFileStatus.FAILED:
return false;
default:
throw new ArgumentOutOfRangeException();
}
}

public void AbortDownloading()
{
CrossDownloadManager.Current.Abort(File);
}

private void Button_Clicked(object sender, EventArgs e)
{
var url = "http://www.orimi.com/pdf-test.pdf";
DownloadFile(url);
}
}



My file path in android is "/storage/emulated/0/Android/data/com.XYZ.projectName/file/Download"



What should I do to get list of files and display in content page? when tap on file name document should be open in specific application.









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Kz,7q5o9Lv9IMJ W1 p5kDmy2,V7zM HByi5iPq0Lv,cTO82o,caAtlNUezvUTEDVGN1TZRYqi2pQY55GCm2DLYsnSu
7f0e nlF1b8dZ3LS,TWMzS9sHNGmxLFW,6eS,MSexaVh

Popular posts from this blog

Rothschild family

Cinema of Italy