# Flex\_image and bulk import

**URL:** <https://www.ruby-forum.com/t/flex-image-and-bulk-import/105956>\
**Category:** Rails\
**Created:** [June 4, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/flex-image-and-bulk-import/105956 "2007-06-04T23:05:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevin\_evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/50afbb/32.png) [@kevin\_evans](https://www.ruby-forum.com/u/kevin_evans)\
**Post date:** [June 4, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/flex-image-and-bulk-import/105956/1 "2007-06-04T23:05:41Z")

</div>

I’m using the excellent flex\_image plugin for a new project and have  
hit a bit of a road block.

I need to import a large number of images and so have been looking at  
an alternative to just using a simple html form to upload the images  
one at a time.

something like…

fp = open(‘one.jpg’,‘rb’)  
file = fp.read  
p = Picture.create(:data=\>file)

gives an error…

FlexImage::Model::InvalidImage: Uploaded file contains no binary  
data. Be sure that {:multipart =\> true} is set on your form.  
from ./script/…/config/…/config/…/vendor/plugins/flex\_image/  
lib/flex\_image/model.rb:153:in `data=’

Anyone have an idea how I can get round this?

---

<div class="post-metadata">

**Author:** ![kevin\_evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/50afbb/32.png) [@kevin\_evans](https://www.ruby-forum.com/u/kevin_evans)\
**Post date:** [June 4, 2007, 11:22pm UTC](https://www.ruby-forum.com/t/flex-image-and-bulk-import/105956/2 "2007-06-04T23:22:32Z")

</div>

Can you use url to the images you want to import? Flex image is  
supporting that otherwise you can hack the code for local file system.

For urls;

Picture.create(:data =\> “[http://localhost/import/1.jpg](http://localhost/import/1.jpg)”)

Gokhan A.  
[www.sylow.net](http://www.sylow.net)  
kevin evans wrote:

> I’m using the excellent flex\_image plugin for a new project and have  
> hit a bit of a road block.
> 
> I need to import a large number of images and so have been looking at  
> an alternative to just using a simple html form to upload the images  
> one at a time.
> 
> something like…
> 
> fp = open(‘one.jpg’,‘rb’)  
> file = fp.read  
> p = Picture.create(:data=\>file)
> 
> gives an error…
> 
> FlexImage::Model::InvalidImage: Uploaded file contains no binary  
> data. Be sure that {:multipart =\> true} is set on your form.  
> from ./script/…/config/…/config/…/vendor/plugins/flex\_image/  
> lib/flex\_image/model.rb:153:in `data=’
> 
> Anyone have an idea how I can get round this?

---

<div class="post-metadata">

**Author:** ![kevin\_evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/50afbb/32.png) [@kevin\_evans](https://www.ruby-forum.com/u/kevin_evans)\
**Post date:** [June 5, 2007, 4:17pm UTC](https://www.ruby-forum.com/t/flex-image-and-bulk-import/105956/3 "2007-06-05T16:17:38Z")

</div>

This seems to work fine in the batch processor I’m using with FlexImage:

@photo = Photo.new()

@photo.data = File.new("/path/to/image/#{image}")

@photo.save

Where image is just the filename of the image. Took a lot of trial and  
error  
to get to that point, but seems to work fine (this is a little longhand  
because this is just small subset of the batch … I’ve also got a lot  
of  
IPTC/EXIF fiddling in there).

---

<div class="post-metadata">

**Author:** ![kevin\_evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/50afbb/32.png) [@kevin\_evans](https://www.ruby-forum.com/u/kevin_evans)\
**Post date:** [June 5, 2007, 6:02pm UTC](https://www.ruby-forum.com/t/flex-image-and-bulk-import/105956/4 "2007-06-05T18:02:57Z")

</div>

Thanks for reminding me, I forgot I adjusted FlexImage to handle this  
(sorry).

In flex\_image/lib/flex\_image/model.rb , there’s a line (156) in the  
data=(file) method that looks like this:

```
  if file.size > 0

```

The problem I ran into was the one you ran into, file.size doesn’t work  
with  
File.new (apparently) … however, file.stat.size does.

So, I added this above that line:

```
  begin
    size = file.stat.size
  rescue
    size = file.size
  end

```

and replaced that line with:

```
  if size > 0

```

The whole thing:

```
  begin
    size = file.stat.size
  rescue
    size = file.size
  end

  if size > 0

```

Sorry again that I forgot to mention that. And yes, there’s probably a  
more  
elegant way than fiddling directly in the FlexImage code. In fact,  
there’s  
probably a more elegant way to handle this in general (feel free to fill  
me  
in).

---

<div class="post-metadata">

**Author:** ![kevin\_evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/50afbb/32.png) [@kevin\_evans](https://www.ruby-forum.com/u/kevin_evans)\
**Post date:** [June 5, 2007, 11:47pm UTC](https://www.ruby-forum.com/t/flex-image-and-bulk-import/105956/5 "2007-06-05T23:47:19Z")

</div>

Chris,

That works great - thanks again.

Kevin

---

<div class="post-metadata">

**Author:** ![kevin\_evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/50afbb/32.png) [@kevin\_evans](https://www.ruby-forum.com/u/kevin_evans)\
**Post date:** [November 11, 2007, 10:05am UTC](https://www.ruby-forum.com/t/flex-image-and-bulk-import/105956/6 "2007-11-11T10:05:36Z")

</div>

There is an easier way. Just wrap your data in a StringIO object:

file = open(filename, ‘rb’).read  
Photo.create :data =\> StringIO.new(file)

---

<div class="post-metadata">

**Author:** ![kevin\_evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/50afbb/32.png) [@kevin\_evans](https://www.ruby-forum.com/u/kevin_evans)\
**Post date:** [June 5, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/flex-image-and-bulk-import/105956/7 "2007-06-05T17:51:54Z")

</div>

Thanks for the replies guys,

Chris,

I tried your method under irb and get…

> > @picture = Picture.new()  
> > =\> #\<Picture:0x3dfa310 @new\_record=true, @invalid\_image=false,  
> > @attributes={“caption”=\>nil, “data”=\>nil}\>
> 
> > @picture.caption = “A test”  
> > =\> “A test”
> 
> > @picture.data = File.new("/Users/kwe/Desktop/two.jpg")  
> > NoMethodError: undefined method `size' for #<File:/Users/kwe/Desktop/ two.jpg> from ./script/../config/../config/../vendor/plugins/flex_image/ lib/flex_image/model.rb:156:in`data=’  
> > from (irb):3
> 
> >

I’m not that familiar with ruby, my File class does not seem to have  
a .size method !?!?

ie.

> > @f = File.new(’/Users/kwe/Desktop/two.jpg’)  
> > =\> #\<File:/Users/kwe/Desktop/two.jpg\>
> 
> > @f.size  
> > NoMethodError: undefined method `size’ for #\<File:/Users/kwe/Desktop/  
> > two.jpg\>  
> > from (irb):11
> 
> > @f.stat  
> > =\> #\<File::Stat dev=0xe000002, ino=1008872, mode=0100644, nlink=1,  
> > uid=501, gid=501, rdev=0x0, size=305935, blksize=4096, blocks=600,  
> > atime=Tue Jun 05 16:17:54 +0100 2007, mtime=Thu Apr 26 16:35:18 +0100  
> > 2007, ctime=Tue Jun 05 16:17:46 +0100 2007\>
> 
> >

Any ideas?

thanks  
Kevin
