ruby-on-railsrubyruby-on-rails-4open-uri

Ruby: Open URI. Get file name from the remote url


I have a remote URL, that doesn't have any reference to filename.

https://example.site.com/sadfasfsadfasdfasfdsafas/

But on downloading it gives file as 'Intro.pdf'

I would like to get that filename in my ruby code so that I can use it to create the file or send file in requests. As of now, I am sending the hardcode name as attachment.pdf

obj = open(url, :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE)
data = obj.read
send_data data, :disposition => 'attachment', :filename=>"attachment.pdf"

Pls Advice.

Thanks


Solution

  • Check result of meta method:

    p obj.meta
    

    probably it have Content-Disposition header. In this case it can have file name as optional parameter.