Пример #1
0
        async public Task Download(Uri fileUri, Page Card)
        {
            ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

            using (WebClient cln = new WebClient())
            {
                string filename     = "ŚĆŻ";
                string partfilename = string.Empty;
                string type         = string.Empty;
                long   Size         = 0;
                try
                {
                    cln.OpenRead(fileUri);
                    string header_contentDisposition = cln.ResponseHeaders["content-disposition"];
                    filename = new ContentDisposition(header_contentDisposition).FileName;
                    byte[] bytes = Encoding.Default.GetBytes(filename);
                    filename     = Encoding.UTF8.GetString(bytes);
                    partfilename = filename.Substring(0, filename.LastIndexOf('.'));
                    type         = filename.Substring(filename.LastIndexOf('.'));
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                }

                FileSavePicker picker = new FileSavePicker();
                picker.SuggestedFileName = partfilename;
                picker.FileTypeChoices.Add(type.Substring(1), new List <string>()
                {
                    type
                });
                picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;

                var file = await picker.PickSaveFileAsync();

                var DownloadO = new Download(Card.Dispatcher)
                {
                    Filename = file.Name,
                    URL      = fileUri,
                    FileSize = Size
                };
                FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);

                if (status == FileUpdateStatus.Complete)
                {
                    BackgroundDownloader downloader = new BackgroundDownloader();
                    DownloadOperation    download   = downloader.CreateDownload(DownloadO.URL, file);
                    DObject = DownloadO;
                    Player.Download.DownloadList.Add(DObject);

                    download.StartAsync();
                    download.RangesDownloaded += Download_RangesDownloaded;
                }
            }
        }
Пример #2
0
        public static string Get(string disposition)
        {
            if (string.IsNullOrWhiteSpace(disposition))
            {
                return(string.Empty);
            }

            var result = string.Empty;

            try {
                result = new ContentDisposition(disposition).FileName;
                const string B64      = "?B?";
                var          b64Index = result.IndexOf(B64, StringComparison.InvariantCultureIgnoreCase);
                if (b64Index > -1)
                {
                    result = result.Substring(b64Index + B64.Length, result.Length - b64Index - B64.Length).Trim('\"').Replace("?=", "");
                    result = Encoding.UTF8.GetString(Convert.FromBase64String(result));
                }
            } catch {
                foreach (var item in disposition.Split(";").Select(i => i.Trim()))
                {
                    const string FILENAME      = "filename";
                    var          fileNameIndex = item.IndexOf(FILENAME, StringComparison.InvariantCultureIgnoreCase);
                    if (fileNameIndex > -1)
                    {
                        var quotesIndex = item.IndexOf("''", fileNameIndex, StringComparison.InvariantCultureIgnoreCase);
                        if (quotesIndex > -1)
                        {
                            result = item.Substring(quotesIndex + 2, item.Length - quotesIndex - 2);
                        }
                        else
                        {
                            quotesIndex = item.IndexOf("\"", fileNameIndex, StringComparison.InvariantCultureIgnoreCase);
                            if (quotesIndex > -1)
                            {
                                result = item.Substring(quotesIndex + 1, item.Length - quotesIndex - 2);
                            }
                            else
                            {
                                result = item.Substring(fileNameIndex + FILENAME.Length + 1, item.Length - FILENAME.Length - 1);
                            }
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(result))
                    {
                        break;
                    }
                }
            }

            if (string.IsNullOrEmpty(result))
            {
                _logger.Warn($"Не удалось получить имя файла из {disposition}");
            }

            return(HttpUtility.UrlDecode(result));
        }