我可以通过此代码>
将文件下载到SD卡中 DownloadOperation downloadOperation;
CancellationTokenSource cancellationToken;
BackgroundDownloader backgroundDownloader = new BackgroundDownloader();
async private void ButtonDownload_Click(object sender, RoutedEventArgs e)
{
StorageFolder externalDevices = Windows.Storage.KnownFolders.RemovableDevices;
StorageFolder sdCard = (await externalDevices.GetFoldersAsync()).FirstOrDefault();
if (sdCard != null)
{
StorageFile file = await sdCard.CreateFileAsync("Downloads\\d460809d2cef1.pdf", CreationCollisionOption.GenerateUniqueName);
downloadOperation = backgroundDownloader.CreateDownload(new Uri("http://dowmain.com/uploads/d460809d2cef1.pdf"), file);
CancellationTokenSource cts = new CancellationTokenSource();
Progress<DownloadOperation> progress = new Progress<DownloadOperation>(progressChanged);
cancellationToken = new CancellationTokenSource();
try
{
await downloadOperation.StartAsync().AsTask(cancellationToken.Token, progress);
}
catch (TaskCanceledException)
{
TextBlockStatus.Text = "Download canceled.";
downloadOperation.ResultFile.DeleteAsync();
downloadOperation = null;
}
}
}
private void progressChanged(DownloadOperation downloadOperation)
{
int progress = (int)(100 * ((double)downloadOperation.Progress.BytesReceived / (double)downloadOperation.Progress.TotalBytesToReceive));
TextBlockProgress.Text = String.Format("{0} of {1} kb. downloaded - %{2} complete.", downloadOperation.Progress.BytesReceived / 1024, downloadOperation.Progress.TotalBytesToReceive / 1024, progress);
ProgressBarDownload.Value = progress;
switch (downloadOperation.Progress.Status)
{
case BackgroundTransferStatus.Running:
{
TextBlockStatus.Text = "Downloading...";
ButtonPauseResume.Content = "Pause";
break;
}
case BackgroundTransferStatus.PausedByApplication:
{
TextBlockStatus.Text = "Download paused.";
ButtonPauseResume.Content = "Resume";
break;
}
case BackgroundTransferStatus.PausedCostedNetwork:
{
TextBlockStatus.Text = "Download paused because of metered connection.";
ButtonPauseResume.Content = "Resume";
break;
}
case BackgroundTransferStatus.PausedNoNetwork:
{
TextBlockStatus.Text = "No network detected. Please check your internet connection.";
break;
}
case BackgroundTransferStatus.Error:
{
TextBlockStatus.Text = "An error occured while downloading.";
break;
}
}
if (progress >= 100)
{
TextBlockStatus.Text = "Download complete.";
downloadOperation = null;
}
}
我有两个问题:
答案 0 :(得分:0)
您的文件下载代码工作正常。但是这里没有持续的状态更新,
对于您的第二个问题,您可以将PDF文件下载到Windows Phone存储中。
StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
var file= await storageFolder.CreateFileAsync("fileName", CreationCollisionOption.GenerateUniqueName);
其他事情将保持不变。 希望这会奏效,