2017-09-02 20 views
1

我想从ruby脚本上传图像到运行Sinatra的服务器,对于我的生活我无法弄清楚。使用rest-client将图像上传到sinatra

这是我到目前为止。

服务器:

post '/uploads/:filename' do 
    File.open("./uploads/#{params[:filename]}", 'wb') do |f| 
    f.write(params[:filename].read) 
    end 
end 

红宝石脚本:

require 'rest_client' 

RestClient.post("https://localhost:1337/uploads/image.jpg", 
    :filename => File.new("C:\\Users\\ruby\\image.jpg", 'rb')) 

屈是表示错误是:
未定义的方法`为 “image.jpg的” 读”:字符串:

这是有道理的,但我只是不知道我做错了什么。

+0

您可以尝试通过改变File.new给File.open方法? – Mohanraj

+0

同样的错误未定义的方法'read'for“image.jpg”:字符串: – chemical

回答

0

我想通了。

服务器:

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'))