cgi : fileupload.
Nov 10, 2018 22:06:13 GMT 1
Post by rikky on Nov 10, 2018 22:06:13 GMT 1
Since there are so many php and java programs of this kind, but none for BaCon.
Here is an as short, but as clear as possible little program to upload a file from your browser to your webserver.
Put this in your cgi folder of your webserver.
Tested with Apache, but should work with Hiawatha.
In a BaCon webserver, unfortunately, I cannot get this to work, that is still too difficult ;-(
Tested with text files, images and BaCon executables, but should work with all kind of files.
Please let me know.
Here is an as short, but as clear as possible little program to upload a file from your browser to your webserver.
Put this in your cgi folder of your webserver.
Tested with Apache, but should work with Hiawatha.
In a BaCon webserver, unfortunately, I cannot get this to work, that is still too difficult ;-(
Tested with text files, images and BaCon executables, but should work with all kind of files.
Please let me know.
PRINT "Content-type: text/html"
PRINT ""
PRINT "<!DOCTYPE html>"
PRINT "<HTML>"
PRINT "<HEAD>"
PRINT "</HEAD>"
PRINT "<BODY>"
RAW_POST_DATA$ = ""
CONTENT_LENGTH$ = GETENVIRON$("CONTENT_LENGTH")
content_length = VAL(CONTENT_LENGTH$)
CONTENT_TYPE$ = GETENVIRON$("CONTENT_TYPE")
WEBKITFORMBOUNDARY$ = TOKEN$(GETENVIRON$("CONTENT_TYPE"),2,"=")
IF CONTENT_LENGTH$ = "" THEN
WHILE TRUE
stndin = WAIT(STDIN_FILENO, 50)
IF stndin = 0 THEN BREAK
RAW_POST_DATA$ = RAW_POST_DATA$ & CHR$(stndin)
WEND
ELSE
RAW_POST_DATA_memory = MEMORY(content_length + 1)
check = 0
first_part$ = ""
memory_counter = 0
FOR i = 1 TO content_length
stndin = WAIT(STDIN_FILENO, 50)
IF check = 0 THEN
'find 13 10 13 10
first_part$ = first_part$ & CHR$(stndin)
IF RIGHT$(first_part$,4) = CHR$(13) & CHR$(10) & CHR$(13) & CHR$(10) THEN
'now we have some variables extra
IF INSTR(first_part$,"Content-Type:") THEN
'of course there is a Content-Type, there always is.
Content_Type$ = CHOP$(INBETWEEN$(first_part$,"Content-Type:",NL$))
ELSE
Content_Type$ = ""
END IF
IF INSTR(first_part$,"filename=") THEN
'filename$ = CHOP$(INBETWEEN$(first_part$,"filename=","Content-Type:",1),CHR$(34))
filename$ = CHOP$(CHOP$(INBETWEEN$(first_part$,"filename=","Content-Type:",1)),CHR$(34))
ELSE
'this should not be possible
filename$ = ""
END IF
IF Content_Type$ = "application/octet-stream" THEN
'yes we can continue filling the ramdisk without problems.
check = 1
CONTINUE
ELSE
'so far the website always stucks here, no matter what.
'not anymore, hahaha.
check = 1
CONTINUE
END IF
END IF
END IF
IF check = 1 THEN
POKE RAW_POST_DATA_memory + memory_counter,stndin
INCR memory_counter
END IF
NEXT i
END IF
IF RAW_POST_DATA$ = "" AND NOT(MEMCHECK(RAW_POST_DATA_memory)) THEN
PRINT "<BR>"
PRINT "<form id='uploadbanner' enctype='multipart/form-data' method='post' >"
PRINT "<input id='fileupload' name='hela_hola' type='file' />"
PRINT "<input type='submit' value='submit' id='submit' />"
PRINT "</form>"
PRINT "<BR>"
ELSE
IF RAW_POST_DATA$ <> "" THEN
PRINT "RAW_POST_DATA : " & REPLACE$(RAW_POST_DATA$,NL$,"<BR>") & "<BR>"
ELSE
'this is where we come when uploading a file
'be careful, all the other RAW_POST_DATA will be in here too, devided by an extra WEBKITFORMBOUNDARY$
'in that case, things change.
'find the last real byte:
FOR i = content_length TO 1 STEP -1
IF PEEK(RAW_POST_DATA_memory + i) <> 0 THEN BREAK
NEXT i
last_RAW_POST_DATA_memory = i
'so the real thing is :
BSAVE RAW_POST_DATA_memory TO "./" & filename$ SIZE last_RAW_POST_DATA_memory - LEN(WEBKITFORMBOUNDARY$) - 7
'check if file exists
fullpath$ = CHOP$(EXEC$("pwd")) & "/" & filename$
IF FILEEXISTS(fullpath$) THEN
PRINT "File " & filename$ & " uploaded to : " & fullpath$ & "<BR>"
ELSE
PRINT "Uploading failed.<BR>Do you have the right permissions for the folder : " & CHOP$(EXEC$("pwd")) & "?<BR>"
END IF
FREE RAW_POST_DATA_memory
END IF
END IF
PRINT "</BODY>"
PRINT "</HTML>"
END
Rik.