Ruby-on-rails – Ruby On Rails, forced users to download TMP files

I created a file in the tmp directory, which contains the following controller code:

def download
file_path = "#{RAILS_ROOT}/tmp/downloads/xxx.html"
data = render_to_string( :action => :show, :layout => nil )
File.open(file_path, "w "){|f| f << data }
flash[:notice] = "saved to #{file_path}"
end

This will create in the tmp directory I want The required file, what I want to do is to force the user to download the file.

On my local computer, the file is saved to the following path:

 /Users/xxxx/Documents/Sites/xxxx/Website/htdocs/tmp/downloads/xxxx.html

On the real-time server, this URL will be something completely different.

What I want to know is how to force users to download this xxxx.html?

Attachment:
If I put…

redirect_to file_path

…On the controller, it just gives me a notice Find the route.

Cheers.

look at the send_file method. It looks It looks like this:

send_file Rails.root.join('tmp','downloads','xxxxx.html'), :type =>'text/ html', :disposition =>'attachment'

:disposition =>’attachment’ will force the browser to download the file instead of rendering the file. If you want to load in the browser, please set it to “inside If nginx is in front of your Rails application, then you will have to modify your environment configuration (ie. environment / production.rb):

# For nginx :
config.action_dispatch.x_sendfile_header ='X-Accel-Redirect'

I created a file in the tmp directory, which contains the following controller code:

def download
file_path = "#{RAILS_ROOT}/tmp/downloads/xxx.html"
data = render_to_string( :action = > :show, :layout => nil )
File.open(file_path, "w"){|f| f << data }
flash[:notice] = "saved to #{file_path} "
end

This will create the file I want in the tmp directory, all I want to do is force the user to download the file.

On my local computer Above, save the file to the following path:

/Users/ xxxx/Documents/Sites/xxxx/Website/htdocs/tmp/downloads/xxxx.html

On the live server, this URL will be something completely different.

I want to know How to force users to download this xxxx.html?

Attachment:
If I put…

redirect_to file_path

…On the controller, it just gives me a notice The route found.

Cheers.

Look at the send_file method. It looks like this:

< p>

send_file Rails.root.join('tmp','downloads','xxxxx.html'), :type =>'text/html', :disposition =>'attachment' 

: disposition =>'attachment' will force the browser to download the file instead of rendering the file. If you want to load in the browser, set it to "inline". If nginx is in front of your Rails application , Then you will have to modify your environment configuration (ie. environment / production.rb):

# For nginx:
config.action_dispatch.x_sendfile_header = ' X-Accel-Redirect'

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3306 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.