2014-04-09 19 views
1

我捕獲/創建用戶輸入文本從我的應用程序文件,嘗試暫時將它們存儲在我的Heroku tmp目錄下,然後將它們上傳到雲服務,如谷歌驅動器。Tempfile.new與File.open在Heroku

在使用將它視爲我可以上傳成功,但使用File.open當我試圖上傳時出現以下錯誤:

ArgumentError (wrong number of arguments (1 for 0)) 

的錯誤是在電話:

@client.upload_file_by_folder_id(save_path, @folder_id) 

在哪裏@client是與雲服務的會話,save_path是用於上傳的附加文件的位置,@folder_id是他們應該進入的文件夾。

當我使用Tempfile.new我成功地這樣做:

tempfile = Tempfile.new([final_filename, '.txt'], Rails.root.join('tmp','text-temp')) 
    tempfile.binmode 
    tempfile.write msgbody 
    tempfile.close 
    save_path = tempfile.path 
    upload_file = @client.upload_file_by_folder_id(save_path, @folder_id) 
    tempfile.unlink 

File.open代碼:

path = 'tmp/text-temp' 
filename = "#{final_filename}.txt" 
save_path = Rails.root.join(path, filename)  
File.open(save_path, 'wb') do |file| 
    file.write(msgbody) 
    file.close 
end 
upload_file = @client.upload_file_by_folder_id(save_path, @folder_id) 
File.delete(save_path) 

難道說的File.path是一個字符串, Tempfile.path是完整路徑(但不是字符串)?當我拿出每個,他們看起來一樣。

我想使用的文件,因爲我不想改變現有的附件我上傳的文件名,而將它視爲附加到文件名。

任何及所有幫助是極大的讚賞。謝謝!

回答

0

爲了它使用文件的工作,我需要給的save_path設置爲一個字符串:

save_path.to_s