title: How to upload file and save it directly to MongoDB using GridFS
body: <p>I have a Sinatra application hosted on heroku and I'm trying to enable file uploading. I know heroku doesn't allow saving to the file system so I'm trying to save the image to MongoDB using GridFS directly. But I don’t know how.
  Using the code below, I'm able to save to file system</p>
  
  <pre><code>base_dir = Dir.pwd + "/static/images/channels/"
  File.open("#{base_dir}" + params['logo'][:filename], "w") do |f|
    f.write(params['logo'][:tempfile].read)
  end
  </code></pre>
  
  <p>How do I save the file directly to MongoDB without first saving it to the file system?</p>