Cut the Clutter: Test Ignore Files Feature - sign up to become a beta tester here.

Forum Discussion

Vinicius A.2's avatar
Vinicius A.2
New member | Level 1
9 years ago

Upload Corrupted

I'm trying upload a file by http. The Upload is OK. Bas de zip file is always corrupted. I've checked the file before upload and i'ts ok.

My code:

var bytes = File.ReadAllBytes(fileToSend);
                    var fileName = Path.GetFileName(fileToSend);
                    
                    RestClient restClient = new RestClient(uploadUrl);
                    RestRequest request = new RestRequest(Method.POST);

                    request.AddHeader("Content-Type", "application/octet-stream");
                    request.Parameters.Clear();
                    request.AddHeader("Authorization", string.Format("Bearer {0}", token.access_token));

                    var meta = "{\"path\": \"/backups/" + fileName + "\",\"mode\": \"overwrite\",\"autorename\": true,\"mute\": false} ";
                    request.AddHeader("Dropbox-API-Arg", meta);
                    
                    request.AddParameter("application/octet-stream", bytes, ParameterType.RequestBody);
                    request.RequestFormat = DataFormat.Json;

                    var response = restClient.Execute(request);

                    return (response.StatusCode == System.Net.HttpStatusCode.OK);

 

7 Replies

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    9 years ago

    I can't seem to reproduce this. I was just able to upload a zip file using this code and it unzipped properly for me.

    How are you checking the resulting zip file? 

  • Vinicius A.2's avatar
    Vinicius A.2
    New member | Level 1
    9 years ago

    Yes! The File is ok before upload. Using fiddler a notice that in body I have this: System.byte [].

    But if a User addFile to upload a get the error:

    Error in call to API function "files/upload": Bad HTTP "Content-Type" header: "multipart/form-data; boundary=-----------------------------28947758029299".  Expecting one of "application/octet-stream", "text/plain; charset=dropbox-cors-hack".

  • Steve M.'s avatar
    Steve M.
    Icon for Dropbox Staff rankDropbox Staff
    9 years ago

    Could the problem be this line?

    request.RequestFormat = DataFormat.Json;

    Your request format is not JSON, and I wonder if that's causing RestSharp to try to convert the byte array to a string (yielding "System.byte []").

  • Steve M.'s avatar
    Steve M.
    Icon for Dropbox Staff rankDropbox Staff
    9 years ago

    Skimming the source code, I actually don't think this is causing your issue, but I'd still recommend removing that line.

  • Vinicius A.2's avatar
    Vinicius A.2
    New member | Level 1
    9 years ago

    I remove that line, but the problem persists. I think need to upload as "application/octet-stream". This make any sense?

  • Vinicius A.2's avatar
    Vinicius A.2
    New member | Level 1
    9 years ago

    Problem solved with the code below.

     

     WebClient client = new WebClient();
                        client.BaseAddress = "https://content.dropboxapi.com/2/";
                        client.Headers.Add("Content-Type", "application/octet-stream");
                        client.Headers.Add("Authorization", string.Format("Bearer {0}", token.access_token));
                        var meta = "{\"path\": \"/backups/" + fileName + "\",\"mode\": \"overwrite\",\"autorename\": true,\"mute\": false} ";
                        client.Headers.Add("Dropbox-API-Arg", meta);                    
                        var result = client.UploadData("files/upload", WebRequestMethods
                            .Http.Post, bytes);

     

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.6,038 PostsLatest Activity: 6 years ago
415 Following

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

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!