我正在尝试使用其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 调用需要“文件”类型的对象,请弄清楚这一点。任何帮助表示赞赏。谢谢。
模型和视图代码为:
###
#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 %>