我正在尝试从ruby脚本上传一张图片到我运行Sinatra的服务器上,但无论如何我都搞不明白这一点。
这是我到目前为止所拥有的。
服务器:
post '/uploads/:filename' do
File.open("./uploads/#{params[:filename]}", 'wb') do |f|
f.write(params[:filename].read)
end
end Ruby脚本:
require 'rest_client'
RestClient.post("https://localhost:1337/uploads/image.jpg",
:filename => File.new("C:\\Users\\ruby\\image.jpg", 'rb'))Sinatra显示的错误是:
未定义“image.jpg”的方法` `read‘:String:
这是有道理的,但我只是不知道我做错了什么。
发布于 2017-09-06 02:46:15
我想通了。
服务器:
post '/uploads/:filename' do
@filename = File.join("./uploads/", params[:filename])
@datafile = params[:data]
File.open(@filename, 'wb') do |f|
f.write(@datafile[:tempfile].read)
end
end客户端:
RestClient.post("https://server/uploads/#{file}.jpg",
:data => File.open("#{file}.jpg", 'rb'))https://stackoverflow.com/questions/46009673
复制相似问题