我可以下载我的文件,但我最终得到的文件说它无法在我的Windows版本上执行。这是我的代码:
Net::HTTP.start(url_base) do |http|
if response.code == '200'
self.size = response['content-length'].to_i
local_file = save_dir + File::SEPARATOR + strip_file_name(url_path)
if !application_exists?(local_file, response['content-length'].to_i) || overwrite_file?
build_display
self.progress_bar = ProgressBar.new(response['content-length'].to_i, PROGRESSBAR_SIZE_WIDTH)
File.open(local_file, 'w') { |f|
http.get(URI.escape(url_path)) do |str|
f.write str
@counter += str.length
@window.setpos(PROGRESSBAR_LOCATION_TOP, 1)
@window << (self.progress_bar.show(@counter)).center(WINDOW_WIDTH - 2)
@window.refresh
char = @window.getch
http.finish if char == 'c' # cancel download
end
}
end
end
end
如果我使用open-uri下载:
file = open(url,
:content_length_proc => lambda { |content_length|
if content_length && 0 < content_length
self.progress_bar = ProgressBar.new(content_length, PROGRESSBAR_SIZE_WIDTH)
end
},
:progress_proc => lambda { |size|
#@counter += str.length
@window.setpos(PROGRESSBAR_LOCATION_TOP, 1)
@window << (self.progress_bar.show(size)).center(WINDOW_WIDTH - 2)
@window.refresh
char = @window.getch
f.close if char == 'c' # cancel download
},
:read_timeout => 10)
FileUtils::cp(file, local_file)
一切正常......问题是,我需要能够取消下游中流。我怎么能a)以'单块'的方式使用net :: http下载,我认为open-uri可以,或者b)使用open-uri取消下载中流?