Re: Get images or binary files from web form
- Posted by ghaberek (admin) Aug 12, 2022
- 1661 views
Using _setmode() seems to work for me, running xampp-portable-windows-x64-7.4.29-0-VC15 on Windows 10 64-bit with Euphoria 4.1 64-bit. Here's the code I used to test. (Including an override for getenv() that returns a default value and converts strings to atoms.)
#!"C:\Euphoria\bin\eui.exe" include std/io.e include std/convert.e include std/dll.e without warning += { override } with batch constant msvcrt = open_dll( "msvcrt.dll" ) constant _setmode = define_c_func( msvcrt, "_setmode", {C_INT,C_INT}, C_INT ) constant O_BINARY = 0x008000 function set_mode( integer fn, integer mode ) return c_func( _setmode, {fn,mode} ) end function override function getenv( sequence name, object default="" ) object value = eu:getenv( name ) if equal( value, -1 ) then return default end if if atom( default ) then value = to_number( value ) end if return value end function printf( STDOUT, "Content-type: text/html\n\n" ) printf( STDOUT, "<html><head></head><body>\n" ) printf( STDOUT, "<form method=\"post\" action=\"/cgi-bin/upload.esp\" enctype=\"multipart/form-data\">\n" ) printf( STDOUT, "Your text: <input name=\"text\" /><br>\n" ) printf( STDOUT, "Your file: <input type=\"file\" name=\"file\" accept=\"image/png, .jpeg, .jpg, image/gif\"><br>\n" ) printf( STDOUT, "<input type=\"submit\" /></form>\n" ) sequence request_method = getenv( "REQUEST_METHOD" ) if equal( request_method, "POST" ) then sequence content_type = getenv( "CONTENT_TYPE" ) integer content_length = getenv( "CONTENT_LENGTH", 0 ) printf( STDOUT, "<pre><code>\n" ) printf( STDOUT, "CONTENT_TYPE = \"%s\"\n", {content_type} ) printf( STDOUT, "CONTENT_LENGTH = %d\n", {content_length} ) printf( STDOUT, "REQUEST_METHOD = \"%s\"\n", {request_method} ) printf( STDOUT, "</code></pre>\n" ) set_mode( STDIN, O_BINARY ) sequence bytes = get_bytes( STDIN, content_length ) printf( STDOUT, "<p>Received %d bytes!</p>\n", {length(bytes)} ) end if printf( 1, "</body></html>\n" )
I still need to include multipart form handling in Euphoria MVC, but if I recall, last time I looked at it I was stuck on this problem and I believe this corrects it. I will also look at what it takes to do this in the back end.
-Greg