Re: Get images or binary files from web form
- Posted by jmduro in August
- 1484 views
Thanks Greg,
I have not been able to get it work. However, following code works on Linux:
with batch include std/io.e include std/pretty.e include std/get.e include std/convert.e include std/sequence.e -- split include std/text.e -- dequote -- 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 puts(1, "Content-Type: text/plain\n\n") sequence fname = "" sequence content_size = test_val(getenv("CONTENT_LENGTH")) sequence std_input = get_bytes(0, to_integer(content_size)) sequence boundary = {} for n = 1 to length(std_input) do if equal(std_input[n..n+3], "\r\n\r\n") then sequence s = std_input[1..n-1] boundary = split(s, "\r\n") std_input = remove(std_input, 1, n+3) s = split(boundary[2], "filename=") fname = dequote(s[2]) exit end if end for integer p = match("\r\n" & boundary[1], std_input) write_file("/tmp/" & fname, std_input[1..p-1]) printf(1, "Received %s bytes\n", {content_size}) printf(1, "Written %s (%d bytes)", {fname, length(std_input[1..p-1])})
POST response is:
Received 29044 bytes Written bloatbox.zip (28840 bytes)
File is correctly written in /tmp:
root@cpe-tests-tool-JM:/var/www/html# ls -l /tmp/bloatbox.zip -rw-r--r-- 1 www-data www-data 28840 Aug 16 11:14 /tmp/bloatbox.zip root@cpe-tests-tool-JM:/var/www/html# unzip -l /tmp/bloatbox.zip Archive: /tmp/bloatbox.zip Length Date Time Name --------- ---------- ----- ---- 159744 2021-05-24 11:50 Bloatbox.exe 0 2021-05-24 11:50 de/ 8192 2021-05-24 11:50 de/Bloatbox.resources.dll --------- ------- 167936 3 files
Jean-Marc