简体   繁体   中英

Download file and save it in isolated storage

I have following code in my Windows 8/RT app:

public static async Task<StorageFile> SaveAsync(Uri fileUri, StorageFolder folder, string fileName)
    {
        try
        {
            var file = await folder.CreateFileAsync(fileName);
            var downloader = new BackgroundDownloader();
            var download = downloader.CreateDownload(fileUri, file); // here Exeption is fired
            var res = await download.StartAsync();
            return file;
        }
        catch (Exception e)
        {
             Debug.WriteLine(ex.Message);
        }
        return null;
    }

ex.message: Access denied

Note:
file - it's a correct StorageFile ( ContentType=image/jpg )
fileUri - it's correct image uri
folder - it's correct storage folder.

What I missed?

What is the value of folder when you call the code? You may need to add a capability in Package.appxmanifest to the appropriate folder (eg Pictures folder, Documents folder).

By default, the sandboxed nature of a Modern UI app means that it will have explicit access only to its own local folder (think about it like Isolated Storage). In order to access other locations, your manifest file must declare that it needs to access those other locations. The available locations are, however, quite limited.

If you use the Save File Picker, however, you have access to a larger range of locations in which to save your file.

I found little mistake:
I forgot, that fileUri is Uri to local server. Just adding nested property to manifest fixed problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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