How to download files using UWP in c#

后端 未结 1 837
一个人的身影
一个人的身影 2020-12-17 01:17

I\'m a beginner in programming, and I was wondering what the right way is to download files in UWP I now use this, but it only works like 50% of the time:

pu         


        
相关标签:
1条回答
  • 2020-12-17 02:03

    There are two ways to do it.
    First one is to use HttpClient as you do (this works well with small files)

    Second one is to use BackgroundDownloader Class. That's recommended way

     private async void StartDownload_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Uri source = new Uri(inputURL);
    
                StorageFile destinationFile = await KnownFolders.PicturesLibrary.CreateFileAsync(
                    title.Text, CreationCollisionOption.GenerateUniqueName);
    
                BackgroundDownloader downloader = new BackgroundDownloader();
                DownloadOperation download = downloader.CreateDownload(source, destinationFile);
    
                // Attach progress and completion handlers.
                HandleDownloadAsync(download, true);
            }
            catch (Exception ex)
            {
                LogException("Download Error", ex);
            }
        }
    
    0 讨论(0)
提交回复
热议问题