File upload

Mar 25, 2011 at 7:42 PM

Hi,

Could anybody confirm the file upload and download methods with this project  working? The code seems not completed for the upload and download functions?

Many thanks.

Jul 22, 2011 at 1:38 AM

I just got file content download working (as a text file). First, download the most recent version (which gives you a solution file instead of a .dll, making it easier to work with. version I used is here: http://wp7dropbox.codeplex.com/SourceControl/changeset/view/4435

Then, in the following file: [unziped location]\WP7DropBox\WP7DropBox.cs

change the following variable call in the below method from API_ROOT to API_CONTENT_ROOT (the correct version is shown). You need to do this, because API_CONTENT makes the method return the metadata instead of the actual file content.

public void filesget(string path, DownloadProgressChangedEventHandler DownloadProgressChanged, OpenReadCompletedEventHandler OpenReadCompleted)
{
     string requesturl = helper.OAuthRequestUrl(API_CONTENT_ROOT + API_FILES + path.Replace(" ", "%20") + "/", new List<string>());

...

}

You then want to use login information to create a token (as described here http://wp7dropbox.codeplex.com/wikipage?title=User%20log%20in&referringTitle=Documentation). Below is my code, obviously I have cut out my personal dropbox data, you will need to add your own. My code also assumes you are downloading the content of a text file, if your file type is different you should post a reply below if it is working. (code is in following post)

 

 

Jul 22, 2011 at 1:41 AM
Edited Jul 22, 2011 at 2:26 AM

Hopefully this helps people, if you have questions feel free to post them below, but I am by no means an expert (just a curious student). I also need to thank Den Delimarsky and his post http://dotnet.dzone.com/articles/working-files-windows-phone-7?mz=27249-windowsphone7 for helping me with understand decoding a file. now for the code (for some reason the code inserting control is not working, so please forgive the plain text)

 

using System.Net;
using System.Windows;
using l3v5y.Dropbox;
using System.Text;


        void getFiles(string path)
        {
            if(path.Contains(".txt"))
            {
                myDropbox.filesget(path, new DownloadProgressChangedEventHandler(downloadProgressChanged), new OpenReadCompletedEventHandler(txtReadCompleted));
            }
            else if (path.Contains(".jpg"))
            {
                myDropbox.filesget(path, new DownloadProgressChangedEventHandler(downloadProgressChanged), new OpenReadCompletedEventHandler(imgReadCompleted));
            }
        }

        void downloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            TestingTextBlock1.Text = e.TotalBytesToReceive.ToString();
            DownloadSlider.Value = e.ProgressPercentage;
        }

        void txtReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                long length = e.Result.Length;
                byte[] decoded = new byte[length];
                e.Result.Read(decoded, 0, (int)length);
                TestingTextBlock2.Text = UTF8Encoding.UTF8.GetString(decoded, 0, (int)length);
            }
            else
            {
                TestingTextBlock2.Text = e.Error.Message;
            }
        }

        void imgReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                WriteableBitmap myImage = new WriteableBitmap(1600, 1200);
                myImage.LoadJpeg(e.Result);
                testingImage.Source = myImage;
            }
            else
            {
                TestingTextBlock2.Text = e.Error.Message;
            }
        }

//event handler called to start the download
private void Testingbutton_Click(object sender, RoutedEventArgs e)
{
    getFiles("/directory/textFile.txt"); //also would work with getFiles("/directory/image.jpg")
}

 

 

I am using a slider to display how much of the file is downloaded, and the two textblocks to display the file size and then the file content. hopefully this helps some people!

EDIT: this assumes the text file you are looking for is in "Dropbox/directory" and is named "textFile.txt" obviously update that for your own files

EDIT2: method now works with jpgs also, and will convert them into writeable bitmap images, as essentially no conversion is needed