简体   繁体   English

将PDF下载到UWP中的LocalFolder

[英]Download pdf to LocalFolder in UWP

I've a UWP app that downloads a pdf file from a website to ApplicationData.Current.LocalFolder , but when I use the Explorer to open the downloaded file, its size is always 0KB and it can't be opened. 我有一个UWP应用程序,可将pdf文件从网站下载到ApplicationData.Current.LocalFolder ,但是当我使用资源管理器打开下载的文件时,其大小始终为0KB,无法打开。 For downloading, I used following code: 对于下载,我使用了以下代码:

using System.Diagnostics;
using Windows.Networking.BackgroundTransfer;
using Windows.Storage;

private async void Button_Click(object sender, RoutedEventArgs e)
{
    try
    {
        Uri source = new Uri("http://www.sachsen.schule/~goethe-gym-auerbach/vplan/VertretungsplanMo.pdf");
        StorageFile destinationFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("VertretungsplanMo.pdf", CreationCollisionOption.ReplaceExisting);                       
        BackgroundDownloader downloader = new BackgroundDownloader();
        DownloadOperation download = downloader.CreateDownload(source, destinationFile);
        Debug.WriteLine("Download successfull");
    }
    catch (Exception ex)
    {
        Debug.WriteLine("Download error. Exception: " + ex);
    }
}

Although I never get a download error, the file is always 0 KB. 尽管我从未收到下载错误,但文件始终为0 KB。

you are forgetting to call download.StartAsync() 您忘记调用download.StartAsync()

Beside that the BackgroundDownload api's are very powerful because it will make sure the files are downloaded also when the app is not running. 除了BackgroundDownload API之外,它还非常强大,因为它可以确保在应用未运行时也下载文件。 But they are not that easy neither. 但是它们也不是那么容易。 So i recommend to use just the HttpClient for simplicity or check some samples with the backgrounddownloader. 因此,为简单起见,我建议仅使用HttpClient或使用backgrounddownloader检查一些示例。

See https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BackgroundTransfer for more samples around background tranfser 请参阅https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BackgroundTransfer以获取有关背景转换器的更多示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM