Having Issues while upload the doc and pdf file on dropbox

79 views
Skip to first unread message

Paramnoor Singh

unread,
Jan 25, 2017, 7:00:55 AM1/25/17
to Ruby on Rails: Talk, Paramnoor Singh

Problem: I have use the net/http code for uploading the '.doc', '.docx', '.pdf' and images files on dropbox. But I getting the issues like:

1.invalid byte sequence in UTF 8
2. Now while the file is uploading but it not uploading the content and even it uploading as a corrupt file.

Below I writing my code to upload the file on dropbox. Please help me.

Code:

uri = URI.parse("https://content.dropboxapi.com/2/files/upload")
          request = Net::HTTP::Post.new(uri)
          request.content_type = "application/octet-stream"
          request["Authorization"] = "Bearer #{$acess_token}"
          request["Dropbox-Api-Arg"] = "{\"path\":\"/Sass-for-Web-Designers.pdf\"}"
          request.body = ""
          request.body << File.open("app/assets/images/categoryImages/Sass-for-Web-Designers.pdf").read
            #request.body << file.content
            #file.close
          #end
          req_options = {
            use_ssl: uri.scheme == "https",
          }
          response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
            http.request(request)
          end

Walter Lee Davis

unread,
Jan 25, 2017, 7:14:22 AM1/25/17
to rubyonra...@googlegroups.com

> On Jan 25, 2017, at 7:00 AM, Paramnoor Singh <paramnoor...@gmail.com> wrote:
>
>
> Problem: I have use the net/http code for uploading the '.doc', '.docx', '.pdf' and images files on dropbox. But I getting the issues like:
>
> 1.invalid byte sequence in UTF 8
> 2. Now while the file is uploading but it not uploading the content and even it uploading as a corrupt file.
>
> Below I writing my code to upload the file on dropbox. Please help me.
>
> Code:
>
> uri = URI.parse("https://content.dropboxapi.com/2/files/upload")
> request = Net::HTTP::Post.new(uri)
> request.content_type = "application/octet-stream"
> request["Authorization"] = "Bearer #{$acess_token}"
> request["Dropbox-Api-Arg"] = "{\"path\":\"/Sass-for-Web-Designers.pdf\"}"
> request.body = ""
> request.body << File.open("app/assets/images/categoryImages/Sass-for-Web-Designers.pdf").read

This line is opening the file as a UTF-8 string, which it most likely is not. Read up on File.open, particularly the various flags you can pass to it to ensure that the file is being opened as a binary, not a text file.

Walter

> #request.body << file.content
> #file.close
> #end
> req_options = {
> use_ssl: uri.scheme == "https",
> }
> response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
> http.request(request)
> end
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5cff0781-aa79-447d-8518-10d43af536ff%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Paramnoor Singh

unread,
Jan 25, 2017, 9:08:43 AM1/25/17
to Ruby on Rails: Talk, noors...@gmail.com
Yes it was the issue of text. file is reading the doc as text not binary. Thanks for your help

Paramnoor Singh
Reply all
Reply to author
Forward
0 new messages