Re: Get images or binary files from web form

new topic     » goto parent     » topic index » view thread      » older message » newer message
achury said...

Just needed to add platform(), so don't try to run windows specific code on linux machines.

You should be using ifdef instead of platform(). Here's an updated example that uses ifdef to only provide set_mode() on Windows.

#!"C:\Euphoria\bin\eui.exe" 
 
include std/io.e 
include std/convert.e 
include std/dll.e 
 
without warning += { override } 
with batch 
 
ifdef WINDOWS then 
 
    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 
 
end ifdef 
 
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" ) 
 
ifdef WINDOWS then 
    set_mode( STDIN, O_BINARY ) 
end ifdef 
 
    sequence bytes = get_bytes( STDIN, content_length ) 
 
    printf( STDOUT, "<p>Received %d bytes!</p>\n", {length(bytes)} ) 
 
end if 
 
printf( 1, "</body></html>\n" ) 
achury said...

I am also learning about how to parse form response.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

Would be nice to have on standar lib a clever function may scan the binary input, from any web form, separate chunks using boundary, read the Content-Disposition on each section to define how to scan it.

I do plan to add this to Euphoria MVC, although in time I may end up ingesting that entire project into Euphoria standard library.

-Greg

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu