Heroku | Rails 4 | Ruby 2.0 - send_file not presenting file for download in the browser

87 views
Skip to first unread message

vell

unread,
Mar 25, 2014, 10:29:37 PM3/25/14
to rubyonra...@googlegroups.com
Hello all,

I am have an app that is hosted on heroku (rails 4 | ruby 2.0) that I am using paperclip-dropbox to upload attachments to a blog.  I have created a download link for people who read the blog to be able to download the attachments but I am not able to get the file to actually download via the browser. I am curious if anyone knows what could be the issue.

My process is I download the file locally, using HTTParty.get, to public/images and then attempt to present the file to the user to download. Getting the file to download is working without issue. But getting the browser to present the file is my problem.

I am looking for any help on what I could do to figure out what I may be doing incorrectly. 

Here is what I currently have:

app/controllers/blog_attachments.rb

  before_action :set_blog_attachment, only: [:download]

  def download
    download_file(@blog_attachment)
    send_file("#{Rails.root}/public/blog_attachments/#{@blog_attachment.document_file_name}", 
              filename: @blog_attachment.document_file_name,
              type: @blog_attachment.document_content_type,
              stream: true,
              disposition: 'inline')
    respond_to do |format|
      format.js {}
    end
  end

  private
  
  def download_file(file)
    File.open("#{Rails.root}/public/blog_attachments/#{file.document_file_name}",'wb') do |f| 
      f.write HTTParty.get(file.document.url).parsed_response
    end
  end
  
  # Use callbacks to share common setup or constraints between actions.
  def set_blog_attachment
    @blog_attachment = BlogAttachment.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def blog_attachment_params
    params[:blog_attachment]
  end

app/views/blogs/show.html.erb

<% for attachment in @blog.blog_attachments %>
  <%= link_to attachment.document_file_name, download_blog_attachments_path(id: attachment.id), remote: true, method: :get %>
<% end %>

Any help is greatly appreciated.

Thanks,

Frederick Cheung

unread,
Mar 26, 2014, 9:02:26 AM3/26/14
to rubyonra...@googlegroups.com


On Wednesday, March 26, 2014 2:29:37 AM UTC, Vell wrote:

app/views/blogs/show.html.erb

<% for attachment in @blog.blog_attachments %>
  <%= link_to attachment.document_file_name, download_blog_attachments_path(id: attachment.id), remote: true, method: :get %>
<% end %>

don't use remote:true (ajax) here: javascript has not access to the filesystem and so cannot write the file to disk

Fred 
Reply all
Reply to author
Forward
0 new messages