cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to know what we learned at IBC? Check out our learnings on media, remote working and more right here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Download file to local dir

Download file to local dir

Ilya_C
Explorer | Level 3

Hello! I try to Download to disc one file from cloud. I use Dropbox .Net SDK C#. Here my code :

static async Task Download(string folder, string file)
        {
            var dbx = new DropboxClient("TOKEN");
            using (var response = await dbx.Files.DownloadAsync(folder + "/" + file))
            {
                if (response != null)
                    Console.WriteLine("Download start - {0}", response.Response.Name);
                else
                {
                    Console.WriteLine("file not found");
                }

                var files = await response.GetContentAsStreamAsync();
                using (FileStream fileStream = File.Create(response.Response.Name, (int)files.Length))
                {
                    byte[] data = new byte[files.Length];
                    files.Read(data, 0, (int)data.Length);
                    fileStream.Write(data, 0, data.Length);
                }
                Console.WriteLine("file was Downloaded");
            }
        }

I have `Download start ` but never get `file was Downloaded`, why ?

3 Replies 3

chirstius
Dropbox Staff

Hello @Ilya_C,

 

I think part of the problem is in this line:

using (FileStream fileStream = File.Create(response.Response.Name, (int)files.Length))

The stream returned is not seekable, and I think getting the Length requires a seek operation. Can you try removing that and seeing if things work as expected?

 

Also, you might be able to eliminate some code by using CopyTo():

using (FileStream fileStream = File.Create(response.Response.Name))
{
    files.CopyTo(fileStream);
}

Let me know if that helps,

 

-Chuck

eduapp
New member | Level 2

Guys help, I did generate a link to my pdf file, and recently tried to access it from my Android App, using Asynch Task, but it returns nothing and crashes my App so bad. It seems like my App cannot catch the file dropped in my dropbox folder. Note. I can easily download all pdf files from any online source without having an issue. It sucks....please help me..how am i supposed to it?

Greg-DB
Dropbox Staff

@eduapp Your question looks unrelated to this thread, so please start a new thread with the details of the issue you're seeing (e.g., steps and code to reproduce the issue, plus the error message for the crash) so we can take a look and help without spamming anyone else here:

 

https://www.dropboxforum.com/t5/forums/postpage/board-id/101000014

 

Thanks!

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    eduapp New member | Level 2
  • User avatar
    chirstius Dropbox Staff
What do Dropbox user levels mean?