How do I download a binary file over HTTP?
The simplest way is the platform-specific solution:
#!/usr/bin/env ruby
`wget http://somedomain.net/flv/sample/sample.flv`
Probably you are searching for:
require 'net/http'
# Must be somedomain.net instead of somedomain.net/, otherwise, it will throw exception.
Net::HTTP.start("somedomain.net") do |http|
resp = http.get("/flv/sample/sample.flv")
open("sample.flv", "wb") do |file|
file.write(resp.body)
end
end
puts "Done."
Edit: Changed. Thank You.
Edit2: The solution which saves part of a file while downloading:
# instead of http.get
f = open('sample.flv')
begin
http.request_get('/sample.flv') do |resp|
resp.read_body do |segment|
f.write(segment)
end
end
ensure
f.close()
end
How to download binary file
Use response.bodyBytes
:
void downloadFile(File f) async {
var url = Uri.encodeFull('https://API_URL');
var response = await http.post(url, headers: {HttpHeaders.authorizationHeader: 'Bearer '},});
await f.writeAsBytes(response.bodyBytes);
}
I removed the Accept
header as it made no sense. You're hinting the server that you'll expect JSON, whereas you really want a PDF.
How to properly send a binary file over http and download it using javascript?
Make this change and it might work- .then(response => response.blob)
+ .then(response => response.blob())
Regarding the response body
#I can't return raw bytes so I transform it into a string
'body' : output.getvalue().hex(),
you can't send back a hex string, you need to send the raw data. otherwise you would have to convert the hex back to binary as well on the client side before making a blob and later a objectURL
As you may already know the browser will not save an attachment if you fetch it with ajax, what you have to do is "navigate" to the file to trigger the download. And since you are posting json data and converting it to excel you have to do a form submit to send the data over to the server instead of using ajax, since it's not a regular GET request?
<form hidden method="post" enctype="text/plain" action="url">
<textarea name="json">{"a":12}</textarea>
<input type="submit">
</form>
This would be better as you don't have to hold all data in the browsers memory before the file can be saved, besides, you can start saving the file much earlier
Related Topics
Installed Ruby 1.9.3 With Rvm But Command Line Doesn't Show Ruby -V
Difference Between Require_Relative and Require in Ruby
How to Get a Query String from a Url in Rails
How to Generate a List of N Unique Random Numbers in Ruby
What Does To_Proc Method Mean in Ruby
Difference Between Map, Each, and Collect
Understanding Private Methods in Ruby
Strange \N in Base64 Encoded String in Ruby
How to Pass Arguments into a Rake Task With Environment in Rails
Protected and Private Methods in Rails
Natural Language Processing in Ruby
Rvm and Thin, Root Vs. Local User
Error: While Executing Gem ... (Gem::Filepermissionerror)
Validation for Non-Negative Integers and Decimal Values
Regex for Checking the Last Character
Rails Activerecord Perform Group, Sum and Count in One Query