2017-09-02 18 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'))