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

any ideas?

On 14 Aug 2008, at 15:11, John G. 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

Frederick C. wrote:

On 14 Aug 2008, at 15:11, John G. 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

excellent, will give it a go,

thanks

Frederick C. wrote:

On 14 Aug 2008, at 15:11, John G. 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.

On Sep 24, 3:00 pm, Vicky S. [email protected]
wrote:

Frederick C. 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’

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 C. wrote:

On Sep 24, 3:00�pm, Vicky S. [email protected]
wrote:

Frederick C. 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…

Vicky

Fred

On 24 Sep 2008, at 15:52, Vicky S. wrote:

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 C. wrote:

On 24 Sep 2008, at 15:52, Vicky S. wrote:

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.

Thank you for the help Fred.

The permissions were wrong. One more question: why is this error not
logged in the rails log file?

Shantanu.

Fred

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 - RDoc Documentation

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

On Sep 25, 9:23 am, Vicky S. [email protected]