我想通過API REST調用和httmultiparty gem將文件上傳到Box.com。代碼正在工作並上傳到Box.com,但是在將上載的文件寫入服務器文件系統之後,如f.write(data.read)那樣執行該操作,然後捕獲寫入文件的文件路徑作爲Box的輸入參數。 com API REST調用如下所示:filename => File.new(path)。該應用程序將在Heroku上運行,因此我們無法在Heroku的服務器上保存任何文件(只讀),因此我想直接將文件上傳到Box.com,同時繞過在服務器上寫入文件的操作,但不能考慮到Box.com REST調用需要一個「文件」類型的對象。任何幫助表示讚賞。謝謝。文件上傳到box.com而不寫入文件系統
模型和視圖代碼:
###
#The Model
###
class BoxUploader
require 'httmultiparty'
include HTTMultiParty
#base_uri 'https://api.box.com/2.0'
end
class File < ActiveRecord::Base
attr_accessible :file
attr_accessor :boxResponse
FILE_STORE = File.join Rails.root, 'public', 'files'
API_KEY = @myBoxApiKey
AUTH_TOKEN = @myBoxAuthToken
def file=(data) #uploaded file
filename = data.original_filename
path = File.join FILE_STORE, filename
#### would like to bypass the file writing step
File.open(path, "wb") do |f|
f.write(data.read)
end
#############
File.open(path, "wb") do |f|
boxResponse = BoxUploader.post('https://api.box.com/2.0/files/content',
:headers => { 'authorization' => 'BoxAuth api_key={API_KEY&auth_token=AUTH_TOKEN' },
:body => { :folder_id => '911', :filename => File.new(path)}
)
end
end
###
# The View
###
<!-- Invoke the Controller's "create" action -->
<h1>File Upload</h1>
<%= form_for @file, :html => {:multipart=>true} do |f| %>
<p>
<%= f.label :file %>
<%= f.file_field :file %>
</p>
<p>
<%= f.submit 'Create' %>
<% end %>
對box.com的發佈操作應該從您的表單中完成。 – apneadiving