如何从xamarin.forms的“外部文件目录”中获取下载文件的列表?

时间:2018-07-02 09:29:42

标签: android ios xamarin.forms xamarin.ios xamarin.android

我正在使用xamarin.forms。我想下载文件,也想将所有下载的文件显示为列表。我想在android和iOS中都实现此功能。

我从this link参考来下载文件。它已成功实施。

现在,我想列出该文件夹中所有可用的下载文件。

对于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);

            });
        }

对于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);

            });
        }

我在MainActivity和AppDelegate中分别调用了这两个函数。

在PCL内容页面中:

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);
        }
    }

我在android中的文件路径为/storage/emulated/0/Android/data/com.XYZ.projectName/file/Download

我该怎么做才能获取文件列表并显示在内容页面中?点击文件名文件时应在特定应用程序中打开。

0 个答案:

没有答案