Cut the Clutter: Test Ignore Files Feature - sign up to become a beta tester here.
Forum Discussion
Vinicius A.2
9 years agoNew member | Level 1
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
- Mark9 years ago
Super User II
*moves to API forum*
- Greg-DB9 years ago
Dropbox Community Moderator
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.29 years agoNew member | Level 1
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.9 years ago
Dropbox Staff
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.9 years ago
Dropbox Staff
Skimming the source code, I actually don't think this is causing your issue, but I'd still recommend removing that line.
- Vinicius A.29 years agoNew member | Level 1
I remove that line, but the problem persists. I think need to upload as "application/octet-stream". This make any sense?
- Vinicius A.29 years agoNew member | Level 1
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
Find help with the Dropbox API from other developers.6,038 PostsLatest Activity: 6 years ago
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!