Xamarin Android, укажите тип MIME при загрузке файлов с помощью CrossDownloadManager - PullRequest
0 голосов
/ 29 января 2019

Я пытаюсь загрузить файл, который хранится в БД, используя url.

Ниже приведен пример кода для загрузки

            var dirMainToCreate = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Constants.AppDirectory);
            if (!System.IO.Directory.Exists(dirMainToCreate))
            {
                System.IO.Directory.CreateDirectory(dirMainToCreate);
            }


            var dirContentToCreate = System.IO.Path.Combine(dirMainToCreate, contentdirectory);
            if (!System.IO.Directory.Exists(dirContentToCreate))
            {
                System.IO.Directory.CreateDirectory(dirContentToCreate);
           }    


            CrossDownloadManager.Current.PathNameForDownloadedFile = new Func<IDownloadFile, string>(file => {
                return Path.Combine(dirContentToCreate, filename);
            });


            (CrossDownloadManager.Current as DownloadManagerImplementation).IsVisibleInDownloadsUi = true;



           File = CrossDownloadManager.Current.CreateDownloadFile(Constants.Url+"/Files/GetFile?id="+ fileId, new Dictionary<string, string> {
                    { "Authorization", "Bearer "+this.Token }
                 }
            );

           CrossDownloadManager.Current.Start(File);

Файл успешно загружен, но онне может быть открыт.

Пожалуйста, помогите

1 Ответ

0 голосов
/ 07 февраля 2019

Я, наконец, использовал другой подход, используя WebClient

https://www.c -sharpcorner.com / article / how-to-download-files-in-xamarin-forms/

    public void DownloadFile(string url, string token, string folder)
    {
        string pathToNewFolder = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, folder);
        Directory.CreateDirectory(pathToNewFolder);

        try
        {
            using (WebClient client = new WebClient())
            {
                client.Headers.Add("API_KEY", App.Api_key);
                client.Headers.Add("Authorization", "Bearer " + token);



                using (Stream rawStream = client.OpenRead(url))
                {
                    string fileName = string.Empty;
                    string contentDisposition = client.ResponseHeaders["content-disposition"];
                    if (!string.IsNullOrEmpty(contentDisposition))
                    {
                        string lookFor = "filename=";
                        int index = contentDisposition.IndexOf(lookFor, StringComparison.CurrentCultureIgnoreCase);
                        if (index >= 0)
                            fileName = contentDisposition.Substring(index + lookFor.Length);
                    }

                    string pathToNewFile = Path.Combine(pathToNewFolder, fileName);

                    client.DownloadFile(url, pathToNewFile);

                }
            }

        }
        catch (Exception ex)
        {
            if (OnFileDownloaded != null)
                OnFileDownloaded.Invoke(this, new DownloadEventArgs(false));
        }
    }

Единственная проблема здесь в том, что ОС не показывает свой собственный индикатор выполнения загрузки, а также не выдает уведомление о завершении загрузки файла

...