时间:2021-05-22
1.使用ruby eventmachine和em-http-server gem,完成一个简单的提供文件下载功能的HttpServer
2.使用了EM的FileStreamer来异步发送文件,发送文件时先组装了header,然后调用FileStreamer
require 'rubygems'require 'eventmachine'require 'em-http-server'class HTTPHandler < EM::HttpServer::Server attr_accessor :filename, :filesize, :path def process_http_request #send file async if @http_request_method.to_s =~ /GET/ && @http_request_uri.to_s.end_with?(filename) send_data "HTTP/1.1 200 OK\n" send_data "Server: XiaoMi\n" send_data "Connection: Keep-Alive\n" send_data "Keep-Alive: timeout=15\n" send_data "Content-Type: application/octet-stream\n" send_data "Content-Disposition: filename='#{filename}'\n" send_data "Content-Length: #{filesize}\n" send_data "\n" streamer = EventMachine::FileStreamer.new(self, path) streamer.callback { # file was sent successfully close_connection_after_writing } else response = EM::DelegatedHttpResponse.new(self) response.status = 200 response.content_type 'text/html' response.content = "Package HttpServer<br>usage: wget http://host:port/#{filename}" response.send_response end endendEM::run do path = '/tmp/aaa.tar.gz' EM::start_server("0.0.0.0", 8080, HTTPHandler) do |conn| conn.filename = File.basename(path) conn.filesize = File.size(path) conn.path = path endend声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
让我们编写一个简单的Ruby程序。所有的Ruby文件扩展名都是.rb。所以,把下面的源代码放在test.rb文件中。实例#!/usr/bin/ruby-wput
思路:使用rubyeventmachine和em-http-servergem,完成一个简单的提供文件下载功能的HttpServer;使用了EM的FileStr
让我们写一个简单的ruby程序。所有Ruby源文件将以扩展名.rb。因此,把下面的源代码在一个test.rb文件。#!/usr/bin/ruby-wputs"H
本文实例为大家分享了jQuery实现异步上传一个或多个文件的具体代码,供大家参考,具体内容如下首先使用SpringMvc文件上传,需要引入第三方上传文件的jar
使用ajaxfileupload.js实现上传文件功能一、ajaxFileUpload是一个异步上传文件的jQuery插语法:$.ajaxFileUpload(