rails 上传图片(同时上传多张)

 local_path = "#{Rails.root}/public/uploads/matters/"


      dir_path = local_path
      FileUtils.mkdir_p(dir_path) unless Dir.exist?(dir_path)


      unless params[:matter_image].blank?


        params[:matter_image].each_with_index do |mi,index|


          file_type  = mi.original_filename.split(".").last
          new_str    = Time.now.to_i.to_s + ((100..999).to_a.shuffle[0].to_s)
          if file_type == "jpg" or file_type == "png"
            local_file_name = new_str + "." + file_type
            local_file = local_path + local_file_name


            File.open(local_file, "wb") { |f| f.write( mi.read ) }


            m = Matter.new
            #上传
          

            m.local_photo_path = local_path
            m.local_photo_name = local_file_name
            m.image_name = new_str
            m.save
          end


        end



关键之处就在于先确定好一个路径,然后把传过来的参数写进去就好了。

你可能感兴趣的:(rails 上传图片(同时上传多张))