嗨,我见过 httparty,但我不知道如何实现有很多模型文件示例,但是可以使用哪个参数来上传视频文件
问问题
7199 次
4 回答
6
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')
})
于 2012-05-17T12:03:42.410 回答
1
HTTParty 似乎不支持文件上传(至少截至 2009 年):
有人说不支持的 Google 群组: http ://groups.google.com/group/httparty-gem/browse_thread/thread/fe8a3af8c46e7c75
于 2011-03-29T19:21:44.097 回答
1
通过使用 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 => "*/*"
}
)
于 2017-02-02T17:59:22.730 回答
0
这是来自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
于 2020-04-15T12:56:57.123 回答