Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Johan Sørensen's code to do buffered file uploads in rails (See related posts)

Ruby code to do a buffered file write. Writes chunks of a file to disk from a form post. Ala Johan Sørensen.

File.open("<some path>", "wb") do |f|
  while buff = file_field.read(4096)
    f.write(buff)
  end
end

Comments on this post

johan posts on Oct 23, 2006 at 18:24
you don't need the final close, it's a block; ruby does the closing in that (besides it (newfile) probably is a Fixnum anyway due to the IO#write)
jacques posts on Oct 24, 2006 at 07:34
Updated.
alterego posts on Jan 23, 2007 at 20:46
May be better if you added 'f.flush' .. Just to make sure :)

You need to create an account or log in to post comments to this site.


Related Posts