DownLoadFile does not save non Text files correctly

Mar 11, 2011 at 11:50 PM
I posted this earlier but figured I get a response here:
For the Life of me I cannot get this to work in Wp7, i.e. trying to use the callback function to write the data/bytes to isolated storage just writes a file of the length in bytes but it the byte array contains nothing therefore the file is useless. Here is the code (what am I doing wrong here?):

Not sure if this may have a bearing bot I read in another site where they were developing a similar lib that thier dowload routine could only download text files and not data files: "Fixed a bug in the code that only allowed for text based files to be downloaded. Using restClient.DownloadData now instead of Execute to get the response’s raw Data."
I Notices in your routine your using restClient.ExecuteAsync , not sure if this is something you should look at..
* which becons the question if the Upload function uploaded the file correctly in the first place???.

public void RestoreCompleted(byte [] download)
{

IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
//byte[] data = download;
if (file.FileExists("GeoPositions.dat"))
{
file.DeleteFile("GeoPositions.dat");
}

var fs = new IsolatedStorageFileStream("GeoPositions.dat", System.IO.FileMode.Create, file);
try
{
fs.Write(download, 0, download.Length);
}
catch (Exception err)
{

}
finally
{
if (null != fs)
{
fs.Close();
}
MessageBox.Show("Restore from DropBox Completed!");
}


//DataUpload.Visibility = System.Windows.Visibility.Collapsed;



}

Download func excerpt based on your sample :
Note: I declared the engine at the begining of page class.
private void RestoreFromDropBox_Click(object sender, EventArgs e)
{
if (User_api_key == "" && User_api_secret == "")
{
Login();
}
Action<byte[]> callback = RestoreCompleted;

engine.ApiKey = api_key;
engine.ApiSecret = api_secret;
engine.SetToken(User_api_key, User_api_secret);
try
{
engine.DownloadFile("GeoPositions.dat", callback);

}
Sep 4, 2011 at 9:02 PM

I finally got around to working with the Download method, and here's what I uncovered.

First, you may be getting an error message in your buffer, rather than the contents of your file.  Here are 3 errors that I have come across:

1) "{\"error\": \"Authentication failed\"}" means you aren't logged-in to Dropbox

2) "{\"error\": \"File not found\"}" -- the folder and/or filename you specified may be incorrect.

3) "{\"error\": \"Invalid signature. Expected signature base string: GET&http%3A%2F%2Fapi-content.dropbox.com%2F0%2Ffiles%2Fdropbox%2FTheList%2FTheList-save.sdf&oauth_consumer_key%3Dgz72duxmieng6nf%26oauth_nonce%3D9942703%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1315168927%26oauth_token%3Dn4o70dfryvhces2%26oauth_version%3D1.0\"}"  -- this means you have given an invalid file specification, i.e., \\folder\\file.ext Putting the double "\\" in the folder caused this.

So, how do you discover/decode the error messages?  Here's my callback routine:

public override void DownloadComplete(byte[] buffer)
{
    System.Text.Encoding encoding = new System.Text.UTF8Encoding();

    // Check for any errors by converting the first 10 characters of
    // the buffer into a string.  If it is '{\"error\":' then we have a problem.
    var tmp = encoding.GetString(buffer, 0, 9);
    if (tmp.IndexOf("error") > -1)
    {
        // Found an error so let's convert the entire string
        var errorMessasge = encoding.GetString(buffer, 0, buffer.Length);
    }
    else
    {
        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

        using (var isf = new IsolatedStorageFileStream(_downloadFileName, FileMode.Create, isoStore))
        {
            isf.Write(buffer, 0, buffer.Length);
            isf.Flush();
            isf.Close();
        }
    }
}

Chuck