Is there a way to download file with the original name?

973 views
Skip to first unread message

siri...@gmail.com

unread,
Sep 28, 2017, 10:52:12 AM9/28/17
to Shrine
I am using shrine to upload to AWS. I can display the original file name but when I click the link to download the file it is an ID number or something, not the original file name. Is there a way to have it save as the original file name?

Janko Marohnić

unread,
Sep 28, 2017, 12:57:47 PM9/28/17
to Chris Stewart, Shrine
You can pass in the `:response_content_disposition` option when generating a URL, which gets forwarded to AWS SDK and it generates the corresponding query parameter, and then S3 will set the `Content-Disposition` response header to that value. The `Content-Disposition` response header is the one that the browser looks at to determine which download filename it should use.

So, you can do the following:

  record.attachment.url(response_content_disposition: "inline; filename=\"#{record.original_filename}\"")

You can use the default_url_options plugin to pass that option in automatically:

  plugin :default_url_options, store: ->(io, **options) do
    {response_content_disposition: "attachment; filename=\"#{io.original_filename}\""}
  end

Kind regards,
Janko

On Thu, Sep 28, 2017 at 4:52 PM, <siri...@gmail.com> wrote:
I am using shrine to upload to AWS. I can display the original file name but when I click the link to download the file it is an ID number or something, not the original file name. Is there a way to have it save as the original file name?

--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine+unsubscribe@googlegroups.com.
To post to this group, send email to ruby-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/c37fe766-5842-49ed-b034-dbc0678e4a0c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

siri...@gmail.com

unread,
Sep 29, 2017, 8:07:56 AM9/29/17
to Shrine
Thanks for the info! In a Rails app where or what file would this go in?

Janko Marohnić

unread,
Sep 29, 2017, 4:52:17 PM9/29/17
to Chris Stewart, Shrine
Any Shrine plugin should be loaded either in the initializer, if you want it to be loaded globally via `Shrine.plugin`, or inside the class definition of a specific uploader, if you want the plugin to be loaded only for that uploader.

# config/initializers/shrine.rb
Shrine.plugin :upload_url_options, ...

# OR

# app/uploaders/my_uploader.rb
class MyUploader < Shrine
  plugin :upload_url_options, ...
end

Kind regards,
Janko

--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine+unsubscribe@googlegroups.com.
To post to this group, send email to ruby-...@googlegroups.com.

Chris Stewart

unread,
Sep 29, 2017, 5:17:58 PM9/29/17
to Janko Marohnić, Shrine
Awesome thanks. It's working now!
Reply all
Reply to author
Forward
0 new messages