2013-03-24 94 views
3

我正在使用Carrierwave上傳文件。在運行OS X 10.8的開發機器上,我可以上傳文件並按預期下載文件。尾礦的開發日誌中我看到:Carrierwave空文件 - 文件「MyDocument.pdf」無法打開,因爲它是空的

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服務器虛擬機後,我能上傳文件(並驗證它是物理服務器上),然而,當我點擊鏈接下載文件我收到一條消息,說:

「文件」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} 

其他細節 我有一個方法resources_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 %> 

我運行軌道3.2.11,紅寶石1.9.3p194,carrierwave 0.8.0,rmagick 2.13.2

請讓我知道如果您需要任何其他細節。

回答

3

好的。我解決了這個問題。我不得不修改我的配置/環境/ production.rb文件並註釋掉以下行:

config.action_dispatch.x_sendfile_header =「X-SENDFILE」

花了很多時間研究這一個。我希望別人能夠受益。

+0

同樣的問題在這裏,謝謝!我使用的是Nginx,但由於某種原因仍然使用我認爲適用於Apache的「X-sendfile」。我將它設置爲'config.action_dispatch.x_sendfile_header ='X-Accel-Redirect'#for nginx',因爲它應該是,並且它工作正常。 – ryan2johnson9 2016-04-04 23:57:08

相關問題