# Copying a file from a URL to local machine

**URL:** <https://www.ruby-forum.com/t/copying-a-file-from-a-url-to-local-machine/99492>\
**Category:** Rails\
**Created:** [July 23, 2006, 1:08pm UTC](https://www.ruby-forum.com/t/copying-a-file-from-a-url-to-local-machine/99492 "2006-07-23T13:08:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [July 23, 2006, 1:08pm UTC](https://www.ruby-forum.com/t/copying-a-file-from-a-url-to-local-machine/99492/1 "2006-07-23T13:08:13Z")

</div>

Hello,

In my model, I’d like to be able to copy a remote .jpg file from a URL:

http://\<[doman.com](http://doman.com)\>/path/picture.jpg

I’d like to copy this to my local machine in my model. What  
libraries/calls could I make to make this work?

Any help would be appreciated,

Thanks,

Sam.

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [July 23, 2006, 6:31pm UTC](https://www.ruby-forum.com/t/copying-a-file-from-a-url-to-local-machine/99492/2 "2006-07-23T18:31:52Z")

</div>

Sam D. wrote:

> Hello,
> 
> In my model, I’d like to be able to copy a remote .jpg file from a URL:
> 
> http://\<[doman.com](http://doman.com)\>/path/picture.jpg
> 
> I’d like to copy this to my local machine in my model. What  
> libraries/calls could I make to make this work?
> 
> Any help would be appreciated,
> 
> Thanks,
> 
> Sam.

Sam,

The following is what I do to send a file from MY DOMAIN to the client:

Note: Substitute your own mime-type. In this case, p.render is a  
method of PDF::Writer. If you read a file into an object then it should  
do the same thing (I think).

send\_data p.render, :filename =\> “my\_pdf\_filename.pdf”, :type =\>  
“application/pdf”

OR…you can do this, but I have read some people have concerns because  
it exposes your file location and any other file can subsequently be  
received:

```
send_file( path_and_file_name, :type => "application/pdf")

```

Hope that helps! I’m not an expert so maybe some of the other more  
senior people will have a better answer.

Michael

mmodica at cox dot net

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [July 23, 2006, 7:01pm UTC](https://www.ruby-forum.com/t/copying-a-file-from-a-url-to-local-machine/99492/3 "2006-07-23T19:01:41Z")

</div>

> > I’d like to copy this to my local machine in my model. What  
> > libraries/calls could I make to make this work?

Do you mean copy the image for a remote server to the server  
containing the application or from the application server to a users  
local machine?

If it’s the first case you will probably find the net/http and  
open-uri libraries useful (they are in the standard lib) in the latter  
case see the above post.
