Delliardo
Delliardo

Reputation: 185

Ruby Dropbox APP: How to download a word document

I'm having troubles trying to download word documents from a dropbox using an APP controlled by a ruby program. (I would like to have the ability to download any file from a dropbox).

The code they provide is great for "downloading" a .txt file, but if you try using the same code to download a .docx file, the "downloaded" file won't open in word due to "corruption."

The code I'm using:

contents = @client.get_file(path + filename)
open(filename, 'w') {|f| f.puts contents }

For variable examples, path could be '/', and filename could be 'aFile.docx'. This works, but the file, aFile.docx, that is created can not be opened. I am aware that this is simply grabbing the contents of the file and then creating a new file and inserting the contents.

Upvotes: 0

Views: 213

Answers (1)

user94559
user94559

Reputation: 60143

Try this:

open(filename, 'wb') { |f| f.write contents }

Two changes from your code:

  1. I used the file mode wb to specify that I'm going to write binary data. I don't think this makes a difference on Linux and OS X, but it matters on Windows.
  2. I used write instead of puts. I believe puts expects a string, while you're trying to write arbitrary binary data. I assume this is the source of the "corruption."

Upvotes: 1

Related Questions