I am passing disposition: 'attachment' in send_data method to download image and it works flawlessly on all the platforms but iOS. In iOs it is behaving as if disposition: 'inline' was passed. I am stuck and unable to figure it out. It would be really appreciable if any one could help.
I am using Rails 4.2.6 with Ruby 2.3.4
My code in view is
<%= link_to "Download", blog_blogs_download_image_path, data: {turbolinks: false} %>
and in controller
require 'open-uri'
url = 'resource_path'
data = open(url).read
send_data data, filename: "test_photo.jpg", disposition: 'attachment'
AFAIU, that's the way it is. There's no concept of "downloads" in iOS.
Related
I'm trying to stream a large Amazon S3 file to the browser using the Rails send_data method, however because the file is so large, the server runs out of memory and cannot complete the request.
The code looks something like this:
def download
s3_obj.read
end
def download_file
send_data(file.download, :filename => filename, :type => 'application/gzip', :disposition => 'attachment')
end
Is there a way to stream the chunks of the file with send_data so that it's a single file in the browser? the way I understand it is that send_data has to load the entire file into memory, then send all of that at once.
You should use send_file instead of send_data as it allow you to set the buffer and more option.
More information here.
UPDATE
If you want to download from S3, you can do this:
def download
data = open("S3_OBJECT_URL")
send_file data.read, filename: filename, type: "application/gzip", disposition: 'attachment', stream: 'true', buffer_size: '4096'
end
or
redirect_to s3_object.file.url
I've got a simple Rails app that downloads a PDF from a (private) S3 bucket and serves it to the browser.
# app/controllers/file_controller.rb
class FileController < ApplicationController
def send_pdf
s3_file = Aws::S3::Resource.new(
region: "us-east-1",
access_key_id: S3_ACCESS_KEY_ID,
secret_access_key: S3_SECRET_ACCESS_KEY
).bucket('bucket-name').objects({prefix: "file_name"}).first.get.body.string
send_data s3_file, filename: "FileName.pdf", type: 'application/pdf', disposition: 'inline'
end
end
# config/routes.rb
Rails.application.routes.draw do
get 'file', to: 'file#send_pdf', defaults: { format: 'pdf' }
end
When accessing the route directly via URL, it displays the PDF fine.
When opening a link to the route in a new tab, it displays the PDF fine.
When opening a link in the same tab, the PDF data streams as text to the browser instead.
The same behavior occurs in Rails 4 and 5.
I'm probably missing something annoyingly minor here, but how can I get the open in the same tab behavior to properly display the PDF instead of streaming bytes as text?
Update 1:
Chrome gives a Failed to load PDF document error when send_pdf is modified to use send_file instead of send_data. (This error happens regardless of link click or direct route request.)
def send_pdf
# S3 download
temp_file = "#{Rails.root}/tmp/file.pdf"
File.open(temp_file,"wb") do |f|
f.write(s3_file)
f.close
end
send_file temp_file, filename: "FileName.pdf", type: 'application/pdf', disposition: 'inline'
File.delete(temp_file)
end
Have you tried send_file instead of send_data?
http://apidock.com/rails/v4.2.1/ActionController/DataStreaming/send_file
I'm using rails to send a pdf back to the client and in Firefox the file extension is not showing:
My rails code looks like this:
send_data(
pdf,
:type => "application/pdf",
:disposition => "attachment; filename=transcript_#{Time.zone.now.strftime('%m-%d-%Y %H:%M')}.pdf",
# :filename => "transcript_#{Time.zone.now.strftime('%m-%d-%Y %H:%M')}.pdf"
)
I've been trying to set the file name with a combination of the :filename and :disposition key to display the correct filename in the browser. The :filename key doesn't seem to work in Firefox and the :disposition key gives me the picture above.
What do I need to change to get the pdf file extension to be shown in Firefox?
The space (inside of the time format) is throwing off the file name. You need to surround the file name in quotes.
Try this:
:disposition => "attachment; filename=\"transcript_#{Time.zone.now.strftime('%m-%d-%Y %H:%M')}.pdf\"",
^^ ^^
This behavior is explained here: http://kb.mozillazine.org/Filenames_with_spaces_are_truncated_upon_download.
The key point being that
[The space] creates an ambiguity when parsing the header for the filename when the browser has to consider the possibility of internationalized filenames. As Internet Explorer does not have to worry about this, it will parse the filename until the end of the line. Mozilla will not.
this will definitely work
send_data pdf.render, filename: 'transcript_#{Time.zone.now.strftime('%m-%d-%Y %H:%M')}.pdf',
type: 'application/pdf',
disposition: "attachment"
I wrote the following code.
def help_doc
pdf_filename = File.join(Rails.root, "/public/doc.pdf")
send_file(pdf_filename, :filename => "doc.pdf" :type => "application/pdf", :diposition => "inline")
end
It's working, but not as I want. I'd like to view in the browser the pdf, but it's doing download of the document.
I thought that just writing :disposition => "inline" and I could see on the browser the pdf.
Try removing the content disposition. You have a typo in your code, deposition vs disposition, and you're missing a comma after filename.
I'm trying to send a pdf back to the user but I'm having serious problem getting send_file and send_data to work. I created the pdf file as follows:
tmp = Tempfile.new('filled')
new_tmp_path = PDFPrint.fill_form_using_pdftk(template_path, tmp.path)
send_file (new_tmp_path, :filename => 'filled.pdf')
The browser prompts for a download, but the downloaded filled.pdf file has zero byte.
I have verified that new_tmp_path does contain a valid pdf (good, filled content)
I have tried this:
File.open(new_tmp_path, 'r') do |f|
send_data(f.read, :filename => "filled.pdf")
end
But this also gives me the same download->zero-byte problem, while the file on server (new_tmp_path) has perfect content.
Regards,
Try sending a simple file to see if it works
send_file '/path/to.jpeg', :type => 'image/jpeg', :disposition => 'inline'
Read this thread, I think it has everything you need.