Forum Discussion

Ilya_C's avatar
Ilya_C
Explorer | Level 3
7 years ago

Download file to local dir

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 ?

  • 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's avatar
      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?