【问题标题】:how to upload video file with httparty如何使用 httparty 上传视频文件
【发布时间】:2011-02-28 11:30:24
【问题描述】:

您好,我见过httparty,但我不知道如何实现有很多模型文件示例,但是可以使用哪个参数来上传视频文件

【问题讨论】:

标签: ruby-on-rails-3 httparty


【解决方案1】:

HTTMultiParty 支持通过发布多部分 MIME 文档来上传文件。它包装了 HTTParty,因此您可以以相同的方式使用它。对于文件,只需提供一个 File 对象。自述文件中的示例:

require 'httmultiparty'
class SomeClient
  include HTTMultiParty
  base_uri 'http://localhost:3000'
end

response = SomeClient.post('/', :query => {
  :foo      => 'bar',
  :somefile => File.new('README.md')
})

【讨论】:

    【解决方案2】:

    HTTParty 似乎不支持文件上传(至少截至 2009 年):

    有人说不支持的 Google 群组: http://groups.google.com/group/httparty-gem/browse_thread/thread/fe8a3af8c46e7c75

    关于它的未决问题: https://github.com/jnunemaker/httparty/issues/77

    【讨论】:

      【解决方案3】:

      通过使用 RestClient gem,我们可以轻松处理文件上传,但请确保您安装了 rest-client gem。

      我还附上了邮递员的示例请求的屏幕截图。

      控制器中的代码如下,

        res = RestClient::Request.execute(
            method: :post,
            url: "http://www.your_url.com",
            payload: params,
      
            headers: {
                'Content-Type' => "multipart/form-data",
                Authorization: "some_key",
                :Accept => "*/*"
            }
      
        )
      

      enter image description here

      【讨论】:

        【解决方案4】:

        这是来自HTTParty 的多部分示例:

        # If you are uploading file in params, multipart will used as content-type automatically
        
        HTTParty.post(
          'http://localhost:3000/user',
          body: {
            name: 'Foo Bar',
            email: 'example@email.com',
            avatar: File.open('/full/path/to/avatar.jpg')
          }
        )
        

        现在,您可以在控制器中访问上传的文件,如下所示:

        uploaded_io = params[:avatar]
        File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
          file.write(uploaded_io.read)
        end 
        

        【讨论】:

          猜你喜欢
          • 2018-12-18
          • 2016-12-10
          • 1970-01-01
          • 2018-04-21
          • 1970-01-01
          • 2011-04-04
          • 2020-06-27
          • 2016-10-11
          • 2016-10-30
          相关资源
          最近更新 更多