Andrei Zhukouski
Andrei Zhukouski

Reputation: 3506

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?

Upvotes: 0

Views: 887

Answers (2)

Andrei Zhukouski
Andrei Zhukouski

Reputation: 3506

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

Upvotes: 0

ZombieSheep
ZombieSheep

Reputation: 29953

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 (e.g. 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.

Upvotes: 1

Related Questions