我正在使用 Carrierwave 上传文件。在我运行 OS X 10.8 的开发机器上,我可以按预期上传文件并下载文件。跟踪我看到的开发日志:
Started GET "/resources/download_file/11" for 127.0.0.1 at 2013-03-24 06:29:33 -0400
Processing by ResourcesController#download_file as HTML
Parameters: {"id"=>"11"}
Resource Load (0.2ms) SELECT "resources".* FROM "resources" WHERE "resources"."id" = ? LIMIT 1 [["id", "11"]]
Sent file /Users/scervera/rails_apps/mdn/public/uploads/resource/document/11/MyDocument.pdf (0.1ms)
Completed 200 OK in 2ms (ActiveRecord: 0.2ms)
在使用 Capistrano 部署到我的 Ubuntu 12.04 服务器 VM 后,我可以上传文件(并验证它是否在服务器上),但是,当我单击链接下载文件时,我会收到一条消息:
“文件‘MyDocument.pdf’无法打开,因为它是空的。”
跟踪 production.log 显示了这一点:
Started GET "/resources/download_file/1" for 192.168.0.90 at 2013-03-24 06:31:12 -0400
Processing by ResourcesController#download_file as HTML
Parameters: {"id"=>"1"}
Sent file /var/mdnapp/releases/20130322184251/public/uploads/resource/document/1/MyDocument.pdf (0.1ms)
Completed 200 OK in 2ms (ActiveRecord: 0.1ms)
请注意,缺少此 sql 行:
Resource Load (0.2ms) SELECT "resources".* FROM "resources" WHERE "resources"."id" = ?
当我试图打开文件时,它确实是空的。
此外,我尝试将以下内容添加到我的 deploy.rb 中,但虽然这很有帮助,但并没有解决这个特定问题:
set :shared_children, shared_children + %w{public/uploads}
其他详细信息 我有一个资源控制器,其中包含一个名为 download_file 的方法。见下文:
def download_file
@resource = Resource.find(params[:id])
send_file(@resource.document.path,
:type => 'application/pdf',
:disposition => 'attachment',
:url_based_filename => true)
end
在我看来,我有一个调用此方法的链接标签。请参阅下面的查看代码:
<% if @resources.empty? %>
<p>There are no resources available at this time.</p>
<% else %>
<% @resources.each do |resource| %>
<div class="resource_wrap clearfix">
<h1><%= resource.title.html_safe %></h1><h2><%= resource.subtitle.html_safe %></h2>
<p><%= image_tag resource.image_url(:wallet).to_s %><%= resource.description.html_safe %></p>
<div id="attachments">
<% if resource.document? %>
<p>Get the resource:<%= link_to 'Download File', :action => 'download_file', :id => resource.id %></p>
<% end %>
<% if resource.video? %>
<p>Get the video:<%= link_to 'Download Video', :action => 'download_video', :id => resource.id %></p>
<% end %>
</div>
</div>
<% end %>
<% end %>
我正在运行 rails 3.2.11、ruby 1.9.3p194、carrierwave 0.8.0、rmagick 2.13.2
如果您需要任何其他详细信息,请告诉我。