我已经与这个问题斗争了 2 天,但无法克服以下步骤。
我正在尝试使用 Wistia API 上传视频,但在将文件名和数据从视图传递到控制器的那一刻卡住了。
我正在使用官方http://wistia.com/doc/upload-api#examples_using_ruby API 文档,但不断收到“没有将符号隐式转换为字符串”错误。
我真的很感激任何帮助或提示。
我的观点 index.html.erb:
<h1>File Upload</h1>
<%= form_tag('/uploads', method: :post, multipart: true) do %>
<%= file_field 'pic', 'data' %></p>
<%= submit_tag "Upload" %>
<%= debug params %>
<% end %>
我的控制器上传控制器:
class UploadsController < ApplicationController
def home
end
def uploadFile
end
def create
@xyz = post_video_to_wistia(:pic, :data)
end
require 'net/http'
require 'net/http/post/multipart'
def post_video_to_wistia(name, path_to_video)
uri = URI('https://upload.wistia.com/')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
# Construct the request.
request = Net::HTTP::Post::Multipart.new uri.request_uri, {
'api_password' => '<API_PASSWORD>',
'contact_id' => '<CONTACT_ID>', # Optional.
'project_id' => '<PROJECT_ID>', # Optional.
'name' => '<MEDIA_NAME>', # Optional.
'file' => UploadIO.new(
File.open(path_to_video),
'application/octet-stream',
File.basename(path_to_video)
)
}
# Make it so!
response = http.request(request)
return response
end
end
参数:
{"utf8"=>"✓",
"authenticity_token"=>"/dRprM7ZjQzzb9N9OErk0Dy4tSt/+zsUldpOed8jCqY=",
"pic"=>{"data"=>#<ActionDispatch::Http::UploadedFile:0x3ef23e0 @tempfile=#<Tempfile:C:/Users/Maciek/AppData/Local/Temp/RackMultipart20140527-7076-bo3tzy>,
@original_filename="example.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"pic[data]\"; filename=\"example.jpg\"\r\nContent-Type: image/jpeg\r\n">},
"commit"=>"Upload"}