We're making changes to the Community, so you may have received some notifications - thanks for your patience and welcome back. Learn more here.

Forum Discussion

Gustavo L.5's avatar
Gustavo L.5
New member | Level 1
9 years ago

Can't upload xlsx file to dropbox app

Hi guys, as the title says, I'm unable to upload an xlsx file, I can successfully upload other formats as xls, pdf, ppt, etc. but this gives an error in the moment I'm trying to upload it.

 

The call is via AJAX (jQuery), something like this:

$.ajax({
url: 'https://content.dropboxapi.com/2/files/upload',
type: "POST",
processData: false,
headers: {
"Authorization": token,// My app token
"Dropbox-API-Arg": JSON.stringify(arg),
"Content-Type": "application/octet-stream"
},
data: b64toBlob(f.content, f.type),
success = ...

 

JSON.stringify(arg) gives:

{"path":"/test_api1425/Base referencia histórica.xlsx","mode":"overwrite","autorename":true,"mute":false}

f.type is "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" (the same for an old xls file).

f.content is a base64 encodede string which I convert to blob before sending.

And the error I get is "POST https://content.dropboxapi.com/2/files/upload 400 (Bad Request)"

As I said, this setup works with any file, except *.xlsx files, since I've seen here in the formus this excel files have some issues, I wonder if this has something to do and if you're able to reproduce it, many thanks for your help.

Cheers,
Gustavo.

 

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    Hi Gustavo, the output you shared is just the status line. The body of the response should contain a more useful error message. Please check that and share it here if you still need help with it.

  • Gustavo L.5's avatar
    Gustavo L.5
    New member | Level 1

    Playing a little more with this I noticed the error is because of some "special" characters in the json sent (specifically the path of the file). So this seems like an encoding issue, when I send a file which contains an 'ó' in the name, it gives this error:

    {"readyState":4,"responseText":"Error in call to API function \"files/upload\": HTTP header \"Dropbox-API-Arg\": could not decode input as JSON","status":400,"statusText":"Bad Request"}

    but if I change that 'ó' by an 'o' (with no accent) it works well.

     

    I've been playing with contentType parameter in the ajax call but I can't make it work, any ideas?

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    Yes, for these calls with the parameters in the header, you need to escape these characters. That is, when you use the “Dropbox-API-Arg” header, you need to make it “HTTP header safe”. This means using JSON-style “\uXXXX” escape codes for the character 0x7F and all non-ASCII characters.

    Some, but not all, languages/libraries do this for you. For example, for JavaScript, to do this yourself you could do something like this:

    var charsToEncode = /[\u007f-\uffff]/g;
    function http_header_safe_json(v) {
    return JSON.stringify(v).replace(charsToEncode,
    function(c) {
    return '\\u'+('000'+c.charCodeAt(0).toString(16)).slice(-4);
    }
    );
    }

    and then:

    "Dropbox-API-Arg": http_header_safe_json(arg)
  • Gustavo L.5's avatar
    Gustavo L.5
    New member | Level 1

    That solved the problem, didn't know about the safe header thing, thanks!

    Cheers,
    Gustavo.

About Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,875 PostsLatest Activity: 3 hours ago
323 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!