Dun
Dun

Reputation: 449

docx file corruption using byte stream and Dropbox API

We have a web app that allows users to upload files to their Dropbox account. This web app uses the Dropbox API to facilitate the upload process. After upload, when a user tries to view file type .docx it gives a message, "The file "somefile.docx" cannot be opened because there are problems with the content".

Here is some of the code we are using:

First, we are converting the file to a byte[] and passing it into the API method call.

public static string DropboxUpload(byte[] DBbyte, string filename, string token, string tokensecret)
    {
        try
        {
            for (int i = 0; i < 4; i++)
            {
                var dropclient = new RestClient(FILEURL);
                dropclient.ClearHandlers();
                dropclient.AddHandler("*", new JsonDeserializer());

                dropclient.BaseUrl = FILEURL;
                dropclient.Authenticator = new OAuthAuthenticator(dropclient.BaseUrl, API_KEY, API_SECRET, token, tokensecret);

                var request = new RestRequest(Method.POST);
                request.Resource = VERSION + "/files/dropbox" + PATH;
                request.AddParameter("file", filename);

                request.AddFile(new FileParameter { Data = DBbyte, FileName = filename, ParameterName = "file" });

                var response = dropclient.Execute(request);

                if (response.StatusCode == HttpStatusCode.OK)
                    break;
                else
                    Thread.Sleep(1000);   
            }

            string dropboxLink = GetPublicLinks(filename, token, tokensecret);
            dropboxLink = dropboxLink.Replace("\"", "");
            return dropboxLink;
        }
        catch
        {
            return "";
        }
    }

The response from the api is { "Winner!"} We have also verified that the byte[] is not corrupted before it is sent to Dropbox.

Then when a user tries to open the file either by downloading it from the website or just viewing the file directly from the Dropbox folder, they get this error message. enter image description here

This is happening for .xlsx (Excel 2007 - up) files as well. Files of type .docx and .xlsx are being corrupted when they are uploaded to the Dropbox folder by the Dropbox API? Any help greatly appreciated.

Upvotes: 6

Views: 1197

Answers (1)

user1152631
user1152631

Reputation: 55

.docx files and other office 2007 file types have this issue online. Have you checked your server MIME types (edit - if it uses your server as an in-between)?

Upvotes: 0

Related Questions