Re: Get images or binary files from web form

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

Following code works with files containing no ESC char, so it should work on Linux for all files:

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 (std_input[n] = '\n') and (std_input[n+1] = '\n') then 
    sequence s = std_input[1..n-1] 
    boundary = split(s, '\n') 
    std_input = remove(std_input, 1, n+1) 
    s = split(boundary[2], "filename=") 
    fname = dequote(s[2]) 
    exit 
  end if 
end for 
write_file(fname, std_input)  
printf(1, "Received %s bytes\n", {content_size})  
printf(1, "Written %s (%d bytes)", {fname, length(std_input)})  

Received 29044 bytes 
Written bloatbox.zip (64 bytes) 

Unfortunately, there is no solution for Windows when the file contains ESC chars.

Jean-Marc

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

Search



Quick Links

User menu

Not signed in.

Misc Menu