You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

HairyWombat's avatar
HairyWombat
Helpful | Level 6
2 years ago

Problem downloading file using Dropbox-provided URL

Hi

 

1. I'm developing an app for iOS and Android. Users can enter the url of a file they would like to download. They might choose a Dropbox url like https://www.dropbox.com/s/fzv989hz5mazhqw/ReplacementDatabase.dbf?dl=0. My app does not use Dropbox API and successfully downloads files from other websites but, if my app changes ?dl=0 to ?dl=1 in the url (as suggested by Dropbox docs), the download does not succeed and instead my app receives a small html file saying there is a problem at Dropbox and they will get back to me. Shouldn't this work? It works in a web browser if I make this substitution.

 

2. If this is not supposed to work, is there a way in the Dropbox API to download a file from a url like https://www.dropbox.com/s/fzv989hz5mazhqw/ReplacementDatabase.dbf?dl=1 ?  That is, when the filename is unknown.

 

Thanks

Bill

  • Hi Greg-DB

     

    Hey I really appreciate the effort you and the team have put in.

     

    What I found was:

     

    1. Yes, curl works with the url - I switched from Mac to Windows and curl worked. Bash both times.

     

    2. Fiddler also works with the URL.

     

    3. My published code fails using automatic redirect. It works for you - the difference may be that I'm using Microsoft Xamarin on Mac.

     

    4. In the second redirect, Dropbox proposes https://uc012d2e25383f12de25c9db8029.dl.dropboxusercontent.com/cd/0/get/B-Dfp2veRgmAiBVuB2GBdOb2SPrjBQh9t0CkeebfBm8SdzO4_8rntEJaHCvdDqpc6-v3DZF5AGyupcRw9SOyfq8l_wlZmj63OWNOQpPgmBUcisawx6C0OptaJHDM9IoDPKvPtxaegRX8av8D36sJb1hcObvW7harZlcWcGjIqXz0aw/file?dl=1#    -- which strictly speaking is not a valid url because of the trailing #.

    5. I solved the problem by handling the redirects myself and url-encoding the #.

     

    So, Xamarin could do better. But also, Dropbox really should not be sending invalid urls.  ðŸ™‚

     

    Code below fyi.

     

    Cheers

    Bill

     

    ----------------------

    httpClientHandler = new HttpClientHandler
    {
    AllowAutoRedirect = false
    };

    ----------------------

    try
    {
        // Here change ?dl=0 to ?dl=1   *****
        // webUrl = "https://www.howsmyssl.com/a/check";    // Debug - to check TLS version. It is TLS 1.2. (June 2023)
     
        HttpResponseMessage response = await httpClient.GetAsync ( webUrl );
     
        int redirectCount = 0;
        while ( redirectCount < 20 )
        {
            if ( response.StatusCode == HttpStatusCode.Redirect )
            {
                string redirectUrl;
     
                if ( response.Headers != null && response.Headers.Location != null )
                {
                    if ( response.Headers.Location.IsAbsoluteUri )
                        redirectUrl = response.Headers.Location.AbsoluteUri;
                    else
                   {
                        string[] splits = webUrl.Split ('/' );
                        redirectUrl = "https://" + splits [2] + response.Headers.Location.OriginalString;
                   }
     
                   redirectUrl = redirectUrl.Replace ( "#", "#".UrlEncode () );
                   response = await httpClient.GetAsync ( redirectUrl );
                   if ( response.StatusCode != HttpStatusCode.Redirect )
                       break;
                }
                redirectCount++;
            }
        }
     
        using ( MemoryStream memoryStream = new MemoryStream () )
        {
            using ( Stream stream = await response.Content.ReadAsStreamAsync () )
            {
                stream.CopyTo ( memoryStream );
            }
            memoryStream.Seek ( 0, SeekOrigin.Begin );
            bytes = memoryStream.ToArray ();
        }
     
        return bytes;
    }
    catch ( Exception ex )
    {
        // ...
        }
    }

     

    ----------------------

     

     

     


  • HairyWombat wrote:

    ... My app does not use Dropbox API and successfully downloads files from other websites but, if my app changes ?dl=0 to ?dl=1 in the url (as suggested by Dropbox docs), the download does not succeed and instead my app receives a small html file saying there is a problem at Dropbox and they will get back to me. Shouldn't this work? ...


    Hi HairyWombat,

    This should work, if your web client support redirection and the redirection is not forbidden. Dropbox links don't point the target file directly, but make 2 redirects before that. That's why if your client doesn't support redirection, your download is likely to finish in such a way. Make sure redirects are supported properly.

     


    HairyWombat wrote:

    ..., is there a way in the Dropbox API to download a file from a url like https://www.dropbox.com/s/fzv989hz5mazhqw/ReplacementDatabase.dbf?dl=1 ?  That is, when the filename is unknown.

    ...


    Yes, there is such a way. The API access point /2/sharing/get_shared_link_file gives you the file metadata and downloads the content. Using this access point doesn't suppose redirection support and you can provide the link as is (without replacing ?dl=0 to ?dl=1 etc.). The drawback is that application authentication is not yet supported here (i.e. your application has to be user authenticated - explicitly signed in user and OAuth flow - doesn't matter that it's meaningless).

    Hope this helps.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,915 PostsLatest Activity: 6 hours ago
333 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!