Re: Get images or binary files from web form
- Posted by jmduro Aug 09, 2022
- 2343 views
Here is your code reviewed. It writes the received file in place. Just add a header FILE_NAME in the post request. It will bee seen by the CGI script as HTTP_FILE_NAME.
#!eui -- test program for euphoria cgi -- comment/uncomment the following to test the EUINC variable with batch include std/io.e include std/pretty.e include std/get.e include std/convert.e include std/io.e sequence cmd -- if val is a string, return it, otherwise "Undefined" function test_val(object val) if atom(val) then return "Undefined" elsif object(val) then for i = 1 to length(val) do if not atom(val[i]) then return "Invalid string sequence" end if end for return val else return "Invalid type for string" end if return "foozle" -- never reached end function procedure env_dump(sequence val) printf(1, "%s: %s\n", {val, test_val(getenv(val))} ) end procedure puts(1, "Content-Type: text/plain\n\n") puts(1, "Hello!\n\n") puts(1, "COMMAND LINE\n============\n\n") cmd = command_line() for i = 1 to length(cmd) do printf(1, "%d: %s\n", {i, cmd[i]} ) end for puts(1, "\n\nREMOTE INFO\n===========\n") env_dump("REMOTE_IDENT") env_dump("REMOTE_USER") env_dump("REMOTE_HOST") env_dump("REMOTE_ADDR") puts(1, "\n\nSERVER INFO\n===========\n") env_dump("SERVER_SOFTWARE") env_dump("SERVER_NAME") env_dump("GATEWAY_INTERFACE") env_dump("SERVER_PROTOCOL") env_dump("SERVER_PORT") env_dump("REQUEST_METHOD") env_dump("PATH_INFO") env_dump("PATH_TRANSLATED") env_dump("SCRIPT_NAME") env_dump("QUERY_STRING") env_dump("AUTH_TYPE") env_dump("CONTENT_TYPE") env_dump("CONTENT_LENGTH") puts(1, "\n\nBROWSER INFO\n============\n") env_dump("HTTP_ACCEPT") env_dump("HTTP_USER_AGENT") puts(1, "\n\nFILE NAME\n============\n") sequence fname = test_val(getenv("HTTP_FILE_NAME")) flush(1) puts(1, "\n\nSTANDARD INPUT\n=============\n") object content_size sequence std_input = {} object char content_size = getenv("CONTENT_LENGTH") puts(1, "Content_size: ") print(1, content_size) if atom(content_size) then -- content length no definido puts(1, "\nNo standard input\n\n") abort(0) elsif sequence(content_size) then std_input = {} for n = 1 to to_integer(content_size) do integer c = getc(0) std_input = append(std_input, c) end for else -- Unexpected error puts(1, "There is something strange about CONTENT_LENGTH\n\n") abort(0) end if write_file(fname, std_input) printf(1, "\nStandard input received %s bytes", {content_size})
Jean-Marc