Question

I'm doing a backup of my app database to OneDrive. The database records refer to images that are stored in isolated storage. I backup those images too. The database files.

The destinations of the backup file is:

me/skydrive/my_documents/MyCompany/MyApp/MyBackup.bak

The destination of the jpg image files is

me/skydrive/my_documents/MyCompany/MyApp/MyBackup Images/*.jpg

The database restores fine, but the images don't. I've verified that the image is backed up properly on SkyDrive - I can see it and open it fine from SkyDrive. However, when I restore, the file is corrupt. Here's the code I use to restore:

dynamic cmpFolder = await oneDrive.FindFolder("MyCompany", "me/skydrive/my_documents");
dynamic appFolder = await oneDrive.FindFolder(AppName, cmpFolder.id);

string imagesFileName = Path.GetFileNameWithoutExtension(selectedFile.FileName) + " Images";
dynamic imgFolder = await oneDrive.FindFolder(imagesFileName, appFolder.id);

dynamic fileList = await oneDrive.FindFiles(imgFolder.id);

foreach (var fileData in fileList.data)
{
    string fileName = fileData.name;
    var file =
        await wilFolder.CreateFileAsync(
            Path.GetFileName(fileName), CreationCollisionOption.ReplaceExisting);

    var result = await client.BackgroundDownloadAsync(selectedFile.FileID + 
        "/content/", new Uri(@"\shared\transfers\" + fileName, UriKind.Relative));
}

Using ISETool and viewing \shared\transfers, I can see that the file is no longer readable. It's size is about 128k, whereas the original image was much larger.

I've also tried this, which was my original code until I began seeing the problem:

var downloadResult = await client.DownloadAsync(selectedFile.FileID + "/content/");

using (Stream oneDriveStream = downloadResult.Stream)
{ 
    oneDriveStream.Position = 0;
    byte[] imageBytes = new byte[oneDriveStream.Length];
    int count = oneDriveStream.Read(imageBytes, 0, imageBytes.Length);

    using (var s = await file.OpenStreamForWriteAsync())
    {
        oneDriveStream.CopyTo(s);

        // and tried this
        //s.Write(imageBytes, 0, imageBytes.Length);
    }
}

For reference, here's the FindFolder and FindFiles implementations:

public async Task<dynamic> FindFiles(string folderName)
{
    LiveOperationResult filesResult = await client.GetAsync(folderName + "/files");
    dynamic files = filesResult.Result;

    return files;
}

public async Task<dynamic> FindFolder(string folderName, string parentFolder)
{
    LiveOperationResult folderResult = await client.GetAsync(parentFolder + "/files?filter=folders");
    dynamic folders = folderResult.Result;

    foreach (var folder in folders.data)
        if (folder.name == folderName)
            return folder;

    return null;
}

How do I successfully download *.jpg images from my OneDrive folder?

Was it helpful?

Solution

Try this for your download path instead:

var downloadResult = await client.DownloadAsync(selectedFile.FileID + "/picture?type=full");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top