1. How to download a binary file via http? [Solved]

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() 
 
new topic     » topic index » view message » categorize

2. Re: How to download a binary file via http?

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.

new topic     » goto parent     » topic index » view message » categorize

3. Re: How to download a binary file via http?

--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!

new topic     » goto parent     » topic index » view message » categorize

4. Re: How to download a binary file via http?

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

new topic     » goto parent     » topic index » view message » categorize

5. Re: How to download a binary file via http?

SDPringle said...

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.

new topic     » goto parent     » topic index » view message » categorize

6. Re: How to download a binary file via http?

sallecta said...

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

new topic     » goto parent     » topic index » view message » categorize

7. Re: How to download a binary file via http?

Sorry for printf I stupidly said that is used in wavelib... of course there are puts...

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu