我正在尝试将文件上传到Wistia.com。从params获取path_to_video变量的正确方法是什么,因为它是ActionDispatch对象。
控制器是这样的:
def create
post_video_to_wistia(params[:upload][:file].tempfile)
end
上传代码看起来像这个
def post_video_to_wistia(path_to_video)
uri = URI('https://upload.wistia.com/')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post::Multipart.new uri.request_uri, {
'api_password' => [WISTIA_PASSWORD],
'file' => UploadIO.new(File.open(path_to_video),
'application/octet-stream',
File.basename(path_to_video)
)
}
response = http.request(request)
return response
end
以下是参数:
Parameters: {"upload"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0x007fa8201d58d8 @original_filename="123.mp4", @content_type="video/mp4", @headers="Content-Disposition: form-data; name="upload[file]"; filename="123.mp4"rnContent-Type: video/mp4rn", @tempfile=#<File:/var/15741-1xiizbz>>}, "commit"=>"Send", "id"=>"2"}
params[:file]
将为您获取ActionDispatch对象,然后从中获取您需要的任何内容。
我忘记添加时出现了这个错误
mount_uploader :image, ImageUploader
到为其分配图像的模型,其中":image"是文件字段的名称。
如果您的文件字段名称不是"image",则必须更改:image和ImageUploader以反映您的名称,即
mount :picture, PictureUploader