# File\_column do download from URL

**URL:** <https://www.ruby-forum.com/t/file-column-do-download-from-url/65392>\
**Category:** Rails\
**Created:** [August 17, 2006, 10:19pm UTC](https://www.ruby-forum.com/t/file-column-do-download-from-url/65392 "2006-08-17T22:19:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![iktorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b487fb/32.png) [@iktorn](https://www.ruby-forum.com/u/iktorn)\
**Post date:** [August 17, 2006, 10:19pm UTC](https://www.ruby-forum.com/t/file-column-do-download-from-url/65392/1 "2006-08-17T22:19:04Z")

</div>

Is there a simple way of forcing file\_column to download file from given  
(ex. in form) url instead of uploading it manualy?

I know:  
[http://blog.caboo.se/articles/2006/01/09/file\_column-magick-and-versions](http://blog.caboo.se/articles/2006/01/09/file_column-magick-and-versions)

require ‘open-uri’  
Attachment.find\_first.filename = open(url)  
if not working as expected… what is interesting is that:

> > @a = open(‘[http://www.google.com/intl/pl\_ALL/images/logo.gif](http://www.google.com/intl/pl_ALL/images/logo.gif)’)  
> > =\> #StringIO:0x3c22390  
> > @a = open(‘[http://research.microsoft.com/~dcr/art/lamps/collage/P0002190.JPG](http://research.microsoft.com/~dcr/art/lamps/collage/P0002190.JPG)’)  
> > =\> #\<File:C:/DOCUME~1/VALUED~1/LOCALS~1/Temp/open-uri356.2\>

smaller files return StringIO  
larger - File (Tempfile to be specific)

Any hints?

---

<div class="post-metadata">

**Author:** ![iktorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b487fb/32.png) [@iktorn](https://www.ruby-forum.com/u/iktorn)\
**Post date:** [August 18, 2006, 6:31am UTC](https://www.ruby-forum.com/t/file-column-do-download-from-url/65392/2 "2006-08-18T06:31:36Z")

</div>

using your example

look at  
@a =  
open('[http://www.google.com/intl/pl\_ALL/images/logo.gif](http://www.google.com/intl/pl_ALL/images/logo.gif)’[http://www.google.com/intl/pl\_ALL/images/logo.gif’](http://www.google.com/intl/pl_ALL/images/logo.gif%27)  
)  
@a.instance\_values

you can extract what you need from base\_uri, which does make my way look  
a  
lttle obtuse

let me know how you get on

On 8/18/06, Wiktor — [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

[www.blogsaic.com](http://www.blogsaic.com)  
search, post, blog

---

<div class="post-metadata">

**Author:** ![iktorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b487fb/32.png) [@iktorn](https://www.ruby-forum.com/u/iktorn)\
**Post date:** [August 18, 2006, 9:27am UTC](https://www.ruby-forum.com/t/file-column-do-download-from-url/65392/3 "2006-08-18T09:27:31Z")

</div>

Dion,

Thanks!

Here is how I fixed it:

In model:

attr\_accessor :url  
before\_save :check\_url

private  
def check\_url  
return if self.url.nil? or self.url.empty?  
file = open(self.url)  
if file.is\_a? StringIO  
file.extend MyIOCompat  
else  
file.extend FileColumn::FileCompat  
end  
self.filename = file  
end

end

module MyIOCompat  
def original\_filename; ‘from\_url’; end  
end

There is no error checking so there may be some 500 errors but I use it  
only for admin purposes so it is ok.

---

<div class="post-metadata">

**Author:** ![iktorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b487fb/32.png) [@iktorn](https://www.ruby-forum.com/u/iktorn)\
**Post date:** [August 18, 2006, 6:16am UTC](https://www.ruby-forum.com/t/file-column-do-download-from-url/65392/4 "2006-08-18T06:16:28Z")

</div>

this is quite easy I have done it at [http://www.blogsaic.com/](http://www.blogsaic.com/)

sign up and upload a tile to see how it works

* * *

you can’t send the url to file column

but, what you can do is upload a file from a url to the same table  
column  
using open-uri

you have to do a bit more than just Attachment.find\_first.filename =  
open(url)

my way is not very optimised, but it does the trick…for now because I  
still want to retain the original file name, and open(url) only uses  
some  
cgi generated name

in the tmp directory

but basically the below code checks if the url is formatted correctly in  
the  
http// format

then it strips the file name from the full file path posted in the  
upload  
form

then it copies the temp file to the file\_column with @tile.img\_name =  
File.new(ff.path)

you’ll need to add these to your environment.rb to use the system/kernel  
commands

#Needed for url uploads  
require ‘open-uri’  
require ‘ftools’  
require ‘fileutils’

this is my upload action

@tile = Tile.new(params[:tile])  
@tile.fk\_tbl\_user\_id = @session[:user].id

```
  orig = params[:tile][:img_path] ----------------- this is from 

```

the  
form at [http://www.blogsaic.com/tile/](http://www.blogsaic.com/tile/)

raise if not orig =~  
/^(http|https)://[a-z0-9]+([-.]{1}[a-z0-9]+)_.[a-z]{2,5}(([0-9]{1,5})?/._)?$/ix

```
   logger.error "???????????? VALUE >> #{orig}"
   response = false
      #if good url format, get the file
      tn = open(orig)
      filename = orig.split("/")

      tp = tn.path
      nf = File.new(tp)
      nf_path = File.split(nf.path)

      f = "#{nf_path[0]}" << "/" << "#{filename.last}"

      FileUtils.touch f

      #sleep 2
      ff = File.new("#{f}")
      FileUtils.copy nf.path, ff.path

      @tile.img_name = File.new(ff.path) #gold

   rescue
      if response == false
        logger.error "???????????? BAD URL >> #{orig}"
      end
   end
 else

```

etc etc then do your normal save stuff…

@tile.save etc etc

[http://www.blogsaic.com/](http://www.blogsaic.com/)

I have some extra stuff that checks if the file is from teh file system  
or a  
URL…but I hope this is enough to get you in the right direction

cheers

dion

On 8/18/06, Wiktor — [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

[www.blogsaic.com](http://www.blogsaic.com)  
search, post, blog
