var uri = new System.Uri("ms-appx:///Assets/FixesViaMail_17Dec.pdf", UriKind.Absolute);
StorageFile file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
此代码运行良好,因为我使用的是我的系统文件路径,但每当我使用此代码时
var uri = new System.Uri("http://www.visa.com/assets/upload/11458/Hotel/Voucher114581423144270.pdf", UriKind.Absolute);
StorageFile file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
然后我得到错误..... 价值不在预期范围内。
请帮帮我
答案 0 :(得分:3)
GetFileFromApplicationUriAsync仅用于从应用程序中加载文件。要从网络上阅读文件,您需要下载它。您可以使用HttpClient(对于小文件)或BackgroundDownloader(对于大文件)类来执行此操作。
var uri = new Uri("http://www.visa.com/assets/upload/11458/Hotel/Voucher114581423144270.pdf");
var httpClient = new HttpClient();
// Always catch network exceptions for async methods
try
{
var response = await httpClient.GetAsync(uri);
// save response out
}
catch
{
// Details in ex.Message and ex.HResult.
}
有关详细信息,请参阅Connecting to an HTTP server using Windows.Web.Http.HttpClient (XAML)。
var uri = new Uri("http://www.visa.com/assets/upload/11458/Hotel/Voucher114581423144270.pdf");
BackgroundDownloader download = new BackgroundDownloader();
DownloadOperation download = downloader.CreateDownload(uri, destinationFile);
有关详细信息,请参阅Transferring data in the background。