Re: Sending a File Over HTTP
- Posted by euphoric (admin) Aug 20, 2013
- 1519 views
I've met with great success!
Client-side code:
public function transfer(sequence fPath) sequence result = "FAIL" object filetxt filetxt = read_file( fPath ) if not atom(filetxt) then sequence data, fname fname = filename( fPath ) data = { { "data", filetxt, fname, "text/plain", ENCODE_NONE }, { "size", sprintf("%d",{length(filetxt)}) } } result = http_post( "http://12.345.678.90/files/transfer.esp?transfer=true&name=" & fname & "&company=" & url:encode(COMPANY_NAME), { MULTIPART_FORM_DATA, data } ) else message_box( "Could not backup '" & licFile & "'\n\nPlease try again later or contact us for help.", "SAT Transfer Error", MB_OK+MB_ICONERROR) end if if not equal(result[2],"SUCCESS!") then ERR_MESSAGE = result[2] else ERR_MESSAGE = "" end if return equal(result[2],"SUCCESS!") end function
Server-side (cgi) code:
#!c:/euphoria4.0.5/bin/eui.exe puts(1,"Content-type: text/html\n\n") without warning include std/filesys.e include std/dll.e include std/get.e include std/machine.e include std/sequence.e include std/io.e include settings.e include shared.e object nbytes, qstr sequence params = "", text, LICENSES = get_setting("LICENSES") nbytes = getenv("CONTENT_LENGTH") qstr = getenv("QUERY_STRING") if atom(qstr) then puts(1,"No query string.") abort(1) else if length(qstr) > 0 then params = get_params( qstr ) else puts(1,"Query string is empty.") abort(1) end if end if if atom(nbytes) then nbytes = "" end if nbytes = value(nbytes) if nbytes[1] = GET_SUCCESS then nbytes = nbytes[2] else puts(1,"Internal error. Please try again later.") abort(1) end if text = get_bytes(0,nbytes) if length(text) > 0 and sequence(params) then sequence cname, fname, buDir, buFile, bfile = "transfer" integer i params[1] = lower(params[1]) i = find("company",params[1]) cname = params[2][i] i = find("name",params[1]) fname = params[2][i] if atom( dir( LICENSES & "\\" & cname & "\\" & bfile ) ) then system( "mkdir \"" & LICENSES & "\\" & cname & "\\" & bfile & "\"", 2 ) end if buDir = LICENSES & "\\" & cname & "\\" & bfile & "\\" buFile = buDir & cname & "-" & fname atom fn = open( buFile, "w" ) if fn = -1 then puts(1,"Could not open remote transfer file!") else puts(fn,text) close(fn) puts(1,"SUCCESS!") end if else puts(1,"Bad params or nothing received.") end if
The only problem (and it's not a problem for me, but might be for someone later down the line), is that variable 'text' needs to be parsed of its contents. Here's an example of what is received:
--rOQZtBFmBcvbVJhYJ13g Content-Disposition: form-data; name="data"; filename="foad.lic" Content-Type: text/plain Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis condimentum lobortis tempus. Morbi eros augue, vestibulum nec ultrices at, aliquet et metus. Quisque et orci sit amet mauris volutpat sagittis. Sed pretium eget ante at consequat. Proin leo mi, accumsan quis vestibulum ac, sagittis varius urna. Morbi eget lorem vel purus ullamcorper laoreet non in lectus. Donec ultrices adipiscing sem nec elementum. Phasellus eu ullamcorper eros, ut tincidunt risus. In fringilla justo quis erat dignissim suscipit. Vivamus in ullamcorper posuere. --rOQZtBFmBcvbVJhYJ13g Content-Disposition: form-data; name="size" 545 --rOQZtBFmBcvbVJhYJ13g--
The content of the file is between the blank line after "Content-Type: text/plain" and second marker (--rOQZtBFmBcvbVJhYJ13g). Do we already have a function that will parse this content?