storing a file download in a temp file

132 views
Skip to first unread message

Erwin

unread,
Sep 14, 2010, 6:24:18 PM9/14/10
to Ruby on Rails: Talk
In my app, I have an URL to download a file from a remote site , when
using this URL in a browser I download the file...

"http://www.anotherdomain.com/resource/download/579633"

but I need to to get the content of this file within my app , as I
must encode it and transfer to another app

if I use this url with open-uri, I get the html page, not the file
itself......
should I execute the download within my app ( how) and store it in a
temp file
then open it and encode ..

how should I proceed to make it as simple as possible ?

thanks for your feedback

Robert Walker

unread,
Sep 14, 2010, 9:58:02 PM9/14/10
to rubyonra...@googlegroups.com
Kad Kerforn wrote:
> In my app, I have an URL to download a file from a remote site , when
> using this URL in a browser I download the file...
>
> "http://www.anotherdomain.com/resource/download/579633"
>
> but I need to to get the content of this file within my app , as I
> must encode it and transfer to another app
>
> if I use this url with open-uri, I get the html page, not the file
> itself......

I'm not following you.

If the above link is a URI representing the file (with the appropriate
content-type) then open-uri should get the file. Of course, if that URI
represents an HTML page then yes, you would get an HTML page.

HTTP is content agnostic. It will simple GET (or POST, PUT, DELETE,
etc.) whatever the URI represents.

OpenURI is just a wrapper around net/http, net/https & net/ftp:
http://www.ruby-doc.org/core/classes/OpenURI.html
--
Posted via http://www.ruby-forum.com/.

Parker Selbert

unread,
Sep 14, 2010, 11:57:20 PM9/14/10
to rubyonra...@googlegroups.com

You are on the right track, but if OpenURI is pulling down an HTML page
then you may be pointing at the wrong place. With that sorted all you
need to do is something like the following:

require 'open-uri'
file = Tempfile.new
file.binmode
open(url, headers) { |data| file.write data.read }

That will give you your content in the file variable, and then you can
encode away.

Erwin

unread,
Sep 15, 2010, 3:17:35 AM9/15/10
to Ruby on Rails: Talk

If the above link is a URI representing the file ... no, it's a
request to anotherdomain.com for getting the resource with ID 579633

when I use this url in a browser : "http://www.anotherdomain.com/
resource/download/579633"
then the file (a pdf file in this case) is automatically downloaded
I guess the site is a RoR site, and my request is processed and the
file is sent using send_file

so, I cannot use this url has a file url directly.... GET "http://
www.anotherdomain.com/resource/download/579633"

Parker Selbert

unread,
Sep 15, 2010, 4:33:58 PM9/15/10
to rubyonra...@googlegroups.com
Kad Kerforn wrote:
> If the above link is a URI representing the file ... no, it's a
> request to anotherdomain.com for getting the resource with ID 579633
>
> when I use this url in a browser : "http://www.anotherdomain.com/
> resource/download/579633"
> then the file (a pdf file in this case) is automatically downloaded
> I guess the site is a RoR site, and my request is processed and the
> file is sent using send_file
>
> so, I cannot use this url has a file url directly.... GET "http://
> www.anotherdomain.com/resource/download/579633"

Unless it is possible to get the exact url that represents the other
resource you're trying to download I can't see how you'll save the
resource locally. Is this a service you have a correspondance with? Look
into how to link directly, maybe contact the site maintainer.

Perhaps somebody else will have a good method for capturing though.

Erwin

unread,
Sep 16, 2010, 2:03:33 AM9/16/10
to Ruby on Rails: Talk
thanks

Yes, I sent them a message asking for an additional API call ( there
is only an upload call..)
I could find the exact url they are calling to get the file for
download... it's an amazon S3 url, so it has keys + protection
no way to play with it...

JoeEd E.

unread,
Feb 12, 2015, 6:49:37 PM2/12/15
to rubyonra...@googlegroups.com
I just wanted to add to this... I know I am 5 years too late, but I
didn't want anyone else to be led astray.

You should be able to pull anything you can download in your browser
into your app using tempfile. I had to play with it for a while, but
something like this worked:

require "open-uri"
url = "whatev.com/file_id"
file_name = "local_filename.pdf"

Tempfile.new(file_name).tap do |file|
file.binmode
file.write(open(url).read)
file.close
end
Reply all
Reply to author
Forward
0 new messages