save blob to file?

hope someone can help with this,

in my mysql table i've got a blob column storing images inside the database, it's also got the filetype and filesize.

now i can work out what the filename will be (e.g. 2002.jpg) but i
don't know how to go thru each of the records and save the blob data to a
file

SomeClass.find(:all).each do |obj|    File.open(obj.filename, 'w') do |f|     f.write obj.blob    end end

Fred

Frederick Cheung wrote:

Frederick Cheung wrote:

hope someone can help with this,

in my mysql table i've got a blob column storing images inside the database, it's also got the filetype and filesize.

now i can work out what the filename will be (e.g. 2002.jpg) but i
don't know how to go thru each of the records and save the blob data to a
file

SomeClass.find(:all).each do |obj|    File.open(obj.filename, 'w') do |f|     f.write obj.blob    end end

Fred

I am trying to save a blob column of MySQL database to a file with following code, but its not working:

    @attachment = Attachment.find(params[:id])     @filename = @attachment.filename

    File.open(File.join(RAILS_ROOT, "tmp", "#{@attachment.id}.doc"), "w") do |file|       file.write(@attachment.data)       end

Can someone help me with this?

Also, when are the contents of tmp directory deleted?

Thanks, Vicky.

Frederick Cheung wrote:

I am trying to save a blob column of MySQL database to a file with following code, but its not working:

@attachment = Attachment\.find\(params\[:id\]\)
@filename = @attachment\.filename

File\.open\(File\.join\(RAILS\_ROOT, "tmp", "\#\{@attachment\.id\}\.doc"\),

"w") do |file| file.write(@attachment.data) end

Can someone help me with this?

what's happening or not happening? On some platforms you might need to change the mode to 'wb'

Also, when are the contents of tmp directory deleted?

Never. RAILS_ROOT/tmp is just conceptually for temporary stuff. Apart from that it's just a normal folder. For actual temporary files (as Tempfile might create) it depends on your platform (and in some cases on the system administration policies in effect on the machine.

Fred

Frederick Cheung wrote:

Frederick Cheung wrote:

Can someone help me with this?

what's happening or not happening? On some platforms you might need to change the mode to 'wb'

I am using OpenSuse OS. I guess 'b' is needed on Windows OS to save it as a binary file. Problem: I don't see any such file on my filesystem.

Also, when are the contents of tmp directory deleted?

Never. RAILS_ROOT/tmp is just conceptually for temporary stuff. Apart from that it's just a normal folder. For actual temporary files (as Tempfile might create) it depends on your platform (and in some cases on the system administration policies in effect on the machine.

So I may need to change my file path. I am saving these files for some further processing and then rendering. So I don't want to store them permanently. Can someone suggest me a way to accomplish this task?

Thanks for the prompt reply..

Frederick Cheung wrote:

Frederick Cheung wrote:

Can someone help me with this?

what's happening or not happening? On some platforms you might need
to change the mode to 'wb'

I am using OpenSuse OS. I guess 'b' is needed on Windows OS to save it as a binary file. Problem: I don't see any such file on my filesystem.

more likely that you don't have write access or you're ending up
building a duff path. Inspect the exact path you're trying to use and
check permissions and so on.

Fred

Frederick Cheung wrote:

The reason no error is thrown has nothing to do with Rails, but Ruby. IO write is the actual method that is being called, correct? See this: Class: IO (Ruby 3.1.2)

According the the Ruby API is returns the number of bytes written. I would suggest you add a check on your write method that the number of bytes > 0 and log an error.

H