Carrierwave空文件 - 无法打开文件“MyDocument.pdf”,因为它是空的

时间:2013-03-24 19:26:26

标签: carrierwave

我正在使用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“,因为它是空的。”

Tailing the 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}

其他详细信息 我有一个resource_controller,其方法名为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

如果您需要任何其他详细信息,请与我们联系。

1 个答案:

答案 0 :(得分:3)

行。我解决了这个问题。我不得不编辑我的config / environments / production.rb文件并注释掉以下行:

config.action_dispatch.x_sendfile_header =“X-Sendfile”

花了很多时间研究这个。我希望其他人可以受益。