【问题标题】:Having Issues while upload the doc and pdf file on dropbox在 Dropbox 上上传 doc 和 pdf 文件时出现问题
【发布时间】:2017-01-25 22:06:37
【问题描述】:

问题:我使用 net/http 代码在 Dropbox 上上传“.doc”、“.docx”、“.pdf”和图像文件。但我遇到的问题如下:

  1. UTF 8 中的字节序列无效。

  2. 现在虽然文件正在上传,但它没有上传内容,甚至上传为损坏的文件。

下面我编写代码以将文件上传到 Dropbox。请帮帮我。

代码:

      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

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    我得到了解决方案。只是因为文件是以文本格式而不是二进制格式读取的。在上面代码中更新的行下方

      request.body <<  File.open("app/assets/images/categoryImages/Sass-for-Web-Designers.pdf", 'rb').read 
    

    谢谢

    【讨论】:

      猜你喜欢
      • 2014-03-21
      • 1970-01-01
      • 2014-04-19
      • 2012-03-10
      • 1970-01-01
      • 2021-08-04
      • 2014-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多