Skip to content

Instantly share code, notes, and snippets.

@jamiehodge
Created August 28, 2013 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiehodge/6365307 to your computer and use it in GitHub Desktop.
Save jamiehodge/6365307 to your computer and use it in GitHub Desktop.
def download
content = Zoid::Content.new
content.write(resource.file.read)
res.body = content
res.body.close
serve
throw :halt
end
@jamiehodge
Copy link
Author

def download
  serve
  res.body << resource.file.read(4096) until resource.file.eof?
end

@judofyr
Copy link

judofyr commented Aug 28, 2013

def download
  # Set the expected number of bytes we'll stream so the user gets a progress bar
  res.body.total_size = resource.size
  # Start serving the response/headers
  serve
  # Stream the body
  res.body << resource.file.read(4096) until resource.file.eof?
  # Finish
  res.body.close
end

@jamiehodge
Copy link
Author

    def download
      res.headers.content_type = resource.type
      res.body.total_size = resource.file.size
      serve
      io = resource.file
      until io.eof? do
        begin
          Fiber.yield (res.body << io.read_nonblock(4096))
        rescue IO::WaitReadable
          IO.select([io])
          retry
        end
      end
      res.body.close
    end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment