2010-04-18 12 views
11

मैं रेल का उपयोग कर एक एमएस-शब्द फ़ाइल भेज रहा हूं। i.e जब मैं किसी लिंक पर क्लिक करता हूं, तो tmp फ़ोल्डर (प्रोजेक्ट में) से एक दस्तावेज़ फ़ाइल भेजी जाती है।रेल में send_file का उपयोग

कोड मैं उपयोग कर रहा हूँ

@filename ="#{RAILS_ROOT}/tmp/test/test.doc" 
send_file(@filename , 
      :filename  => "test", 
      :type   => 'application/msword', 
      :disposition => 'attachment', 
      :streaming => 'true', 
     :buffer_size => '4096') 

यह काम कर रहा है, लेकिन यह एक खाली फ़ाइल भेज रहा है है। फ़ाइल में सामग्री गायब है। कोई सुझाव?

+1

+1 में निम्न पंक्ति। सुनिश्चित करें कि आप एक खाली फ़ाइल नहीं भेजते हैं। – fig

+0

मैं एक खाली फ़ाइल नहीं भेज रहा हूं – sgi

उत्तर

14

कोई send_file नहीं है: स्ट्रीमिंग विकल्प, यह है: स्ट्रीम। आप खराब पैरामीटर प्रकार गुजर रहे हैं। : buffer_size संख्या होना चाहिए, एक स्ट्रिंग नहीं। : स्ट्रीम बूलियन होना चाहिए, स्ट्रिंग नहीं।

:stream => true, 
:buffer_size => 4096, 

आपको केवल फ़ाइल नाम पैरामीटर की आवश्यकता है (यदि आप मूल से दूसरे नाम के साथ फ़ाइल भेजना चाहते हैं)। आपके द्वारा उपयोग किए जा रहे अन्य विकल्प डिफ़ॉल्ट हैं (सिवाय इसके कि: प्रकार)।

क्या आप इसे आजमा सकते हैं?

@filename ="#{RAILS_ROOT}/tmp/test/test.doc" 
send_file(@filename, :filename => "test.doc") 
0

साथ भेजने का प्रयास करें: स्वभाव => 'इनलाइन'

send_file path, :type => 'application/msword', :disposition => 'inline' 
3

टिप्पणी बाहर config/environments/production.rb

config.action_dispatch.x_sendfile_header = "X-Sendfile" 
+0

nginx के लिए यह 'एक्स-एक्सेल-रीडायरेक्ट' है। – m33lky

+0

आह, धन्यवाद। हम nginx का उपयोग करें। – panzi

0
In your view => 
<%= link_to "click here to download", signed_feeds_pdf_path(:feed_image_path => feed_image.feedimage.path), target: '_self' %> 
In your controller => 
    def pdf 
    file_name = params[:feed_image_path].split('/').last 
    @filename ="#{Rails.root}/public/uploads/feed_image/feedimage/#{file_name}" 
    send_file(@filename , 
     :type => 'application/pdf/docx/html/htm/doc', 
     :disposition => 'attachment')   
    end 
संबंधित मुद्दे