简体   繁体   中英

File upload kills rails app and server

I have simple model which looks like this:

def video_file=(input_data)
  unless input_data.to_s.empty?
    newfile = File.open("#{RAILS_ROOT}/public/to_upload/#{self.filename}_vid.f4v", "wb") do |f|
      while buff = input_data.read(4096)
        f.write(buff)
      end
    end
  end
end

and here the error which rails manages to display and then dies, literally.

 ActiveRecord::StatementInvalid in <ControllerName>

Why?

Replace

newfile = File.open(path, "wb") do |f|
while buff = input_data.read(4096)
  f.write(buff)
end

with

if input_data.respond_to?(:read)
  File.open(path, "wb") { |f| f.write(input_data.read) }
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM