1. How to download a binary file via http? [Solved]
- Posted by sallecta Feb 12, 2011
- 1389 views
Hi,
I'm new to programming, and I chose Euphoria to start. There is good example of downloading text file in Euphoria 4 demos folder, but I stuck in getting the binary file.
Here is the changes I've made with wget demo
added new function to write a file in binary mode (actually ripped it from Wavelib 1.0 by Daryl Van Den Brink)
sequence localfile localfile = "localfile.txt" function write__file(sequence outputfile, sequence filedata) integer fn fn = open(outputfile, "wb") if fn = -1 then return -1 end if printf(fn, "%s\n", { filedata[2] }) close(fn) return 0 end function -- write_file
changed the output in main procedure
--printf(1, "%s\n", { data[2] }) write__file(localfile, data)
This works fine with text files but no result with binary. Help me to move from this, please. Any advise or links to documentation will be highly appreciated.
complete code (modified wget demo) to download a binary file via http. Thanks to Mihail121 for pointing me to write_file function.
include std/console.e include std/net/http.e include std/io.e sequence localfile localfile = "downloaded" without warning override procedure abort(integer x) maybe_any_key() eu:abort(x) end procedure procedure main(sequence args = command_line()) if length(args) = 2 then ifdef WIN32_GUI then puts(1, "This program must be run from the command-line:\n\n") end ifdef puts(1, "Usage: eui wget.ex URL\n") abort(1) end if sequence url = args[3] object data = http_get(url) if atom(data) or length(data) = 0 then printf(1, "Could not download %s\n", { url }) abort(2) end if write_file(localfile, data[2], BINARY_MODE) end procedure main()
2. Re: How to download a binary file via http?
- Posted by Mihail121 Feb 12, 2011
- 1356 views
printf is false, check the manual for a function called "write_file", it's what you need to use. "putc" can also be used. Read on the difference/similarity between string and binary data.
3. Re: How to download a binary file via http?
- Posted by sallecta Feb 12, 2011
- 1404 views
--printf(1, "%s\n", { data[2] }) --write__file(localfile, data) write_file(localfile, data[2], BINARY_MODE)
Thank you very much for fast response! I've tested the write_file and successfully downloaded and opened the goggle logo and the crc.zip files
printf is successfully used in wavelib which deals with binary data... I have to read something more to understand why in some cases printf works fine, and with other - don't works)). I've also expected some points to initialize "binary" http transfer mode or base64 encode/decode, but your answer is so simple and it works well. If you have a bit of time, could you briefly explain why it works? Sorry, maybe I'me asking too much...
Thank you once again!
4. Re: How to download a binary file via http?
- Posted by SDPringle Feb 12, 2011
- 1324 views
Unless this behavior has changed, printf terminates with a NUL character in the string. This is normally not a problem but for binary it is.
Shawn Pringle
5. Re: How to download a binary file via http?
- Posted by sallecta Feb 12, 2011
- 1335 views
Unless this behavior has changed, printf terminates with a NUL character in the string. This is normally not a problem but for binary it is.
Shawn Pringle
Thanks, Shawn. Now is all clear for me.
6. Re: How to download a binary file via http?
- Posted by mattlewis (admin) Feb 12, 2011
- 1315 views
printf is successfully used in wavelib which deals with binary data... I have to read something more to understand why in some cases printf works fine, and with other - don't works)). I've also expected some points to initialize "binary" http transfer mode or base64 encode/decode, but your answer is so simple and it works well. If you have a bit of time, could you briefly explain why it works? Sorry, maybe I'me asking too much...
As others have said, printf() makes some assumptions that it's printing text. You can use std lib stuff like write_file(), but for general binary output, you should use puts(). That's what those routines use.
Matt
7. Re: How to download a binary file via http?
- Posted by sallecta Feb 12, 2011
- 1278 views
Sorry for printf I stupidly said that is used in wavelib... of course there are puts...