post '/upload' do
unless params[:file] && (tmpfile = params[:file][:tempfile]) && (name = params[:file][:filename])
return haml(:upload)
end
time = Time.now.to_s
time.gsub!(/\s/, '')
name = time + name
while blk = tmpfile.read(65536)
File.open(File.join(Dir.pwd,"public/uploads", name), "wb") { |f| f.write(tmpfile.read) }
end
'success'
end
Everything goes where expected the files just end up being corrupted.
This bit looks really funky:
I’m guessing you’re trying to read your tempfile a 65536-byte block at a time, and then write those blocks successively to your destination file. But you never write
blk, which is the first block you read; you write the rest of the file (tempfile.read) instead. And even if this loop did write blocks like it should, it opens the file anew for each block, overwriting the old contents! Anyway, I suspect you meant something like this:That said, if you’ve got the file as a temp file (presumably already on your local file system), maybe all you need to do is move that file? It’ll go way faster if that’s the case – if the source and destination are on the same disk, it’s just a matter of swapping some file system pointers, rather than copying all that data.
Hope that helps!