Discussion:
Uploading Files with LiveCode Server
(too old to reply)
Gregory Lypny
2013-12-29 01:01:40 UTC
Permalink
Hello everyone,

The LiveCode website has a tutorial on uploading files to a server with LiveCode Server. Anyone know how to specify the folder on the server where files are to be uploaded? It appears that the default is tmp.

Regards,

Gregory
Pierre Sahores
2013-12-29 02:10:38 UTC
Permalink
Hi Gregory,

Out of a production app.
function uploadImages param1
if param1 is "images"
then answer folder toLocalisation("Merci de sélectionner le dossier contenant le référentiel photo à uploader") && "..."
if it is not "" then
set the directory to it
abIamBusy
put toLocalisation("Upload du référentiel photo en cours") & "..." into fld "message_prompter_Label"
if the platform is not in "macos,win32"
then show grp "message_prompter"
else show grp "message_prompter" with visual effect dissolve
set itemDel to "."
wait 1
repeat for each line l in the files
if the shiftkey is down then exit repeat
if item -1 of l is in "jpg,gif,png,jpeg,ico,cur" then
put the directory & "/" & l into tlFileName
put "" into tForm
put "image/" & item -1 of l into tType
### skip unchanged files : top ###
if HexDigest(url(BIAS & "api/services/views/product_images/" & EditorDirPathID & "/" & l)) is HexDigest(url("binfile:" & tlFileName))
then next repeat
### skip unchanged files : bot ###
put toLocalisation("Upload de la photo" && setquote(l) && "en cours") & "..." into fld "message_prompter_Label"
get libUrlMultipartFormData(tForm, "Upload", "Upload")
if it is not "" then
answer error tolocalisation("Par sécurité, les fichiers dont le nom comporte des caractères accentués ne sont pas autorisés.") && \
tolocalisation("Merci de corriger avant de renouveler votre demande.") & return & it & return & return & l
next repeat
end if
set the httpHeaders to line 1 of tForm
delete line 1 of tForm
put "<file>" & tlFileName into tFile
put "binary" into tEnc
get libUrlMultipartFormAddPart(tForm,"image", tFile, tType, tEnc)
if it is not "" then
answer error tolocalisation("Par sécurité, les fichiers dont le nom comporte des caractères accentués ne sont pas autorisés.") && \
tolocalisation("Merci de corriger avant de renouveler votre demande.") & return & it & return & return & l
next repeat
else
ServerWakeup
post leveos(tForm) to URL MIAS
set the httpHeaders to ""
end if
end if
end repeat
end if
end uploadImages
function rias_upl
if $_POST_RAW is not "" then
set itemdel to "&"
put char 1+offset("=", item -2 of $_POST_RAW) to -1 of item -2 of $_POST_RAW into ActiveSubDomain_PostIn
put "/yourfileserverpath/images/" & ActiveSubDomain_PostIn & "/" into UploadBaseDir_images # Constante 12_A #
put "/yourfileserverpath/custom/" & ActiveSubDomain & "/" & trim(char 8 to -1 of item -2 of $_POST_RAW) & "/" into UploadBaseDir_apps # Constante 12_B #
put item 1 to -3 of $_POST_RAW into POST_RAW
put "" into tRequestArray
set the lineDel to LF
set the itemDel to "="
put word 1 of POST_RAW into tBoundary
put length(tBoundary) into tBoundaryLength
put POST_RAW into tData
put offset(tBoundary, tData) into tOffset
repeat until tOffset = 0
put byte 1 to tOffset - 1 of tData into tPart
delete byte 1 to tOffset + tBoundaryLength - 1 of tData
put "" into tHeaders
put offset(CRLF & CRLF, tPart) into tSplit
if tSplit > 0 then
put trim(byte 1 to tSplit - 1 of tPart) into tHeaders
delete byte 1 to tSplit + 3 of tPart
delete byte -2 to -1 of tPart
split tHeaders by ";"
repeat for each line tKey in the keys of tHeaders
put tHeaders[tKey] into tHeader
repeat for each line tParam in tHeader
put trim(item 1 of tParam) into tVar
put trim(item 2 of tParam) into tVal
if char 1 of tVal = quote
then delete char 1 of tVal
if char -1 of tVal = quote
then delete char -1 of tVal
if tVar is not "" and tVal is not ""
then put tVal into tHeaders[tVar]
end repeat
end repeat
if tHeaders["name"] is not "" then
if tHeaders["fileName"] is not "" then # binary data
put tHeaders["fileName"] into tRequestArray[tHeaders["name"]]["fileName"]
put tPart into tRequestArray[tHeaders["name"]]["binData"]
else put tPart into tRequestArray[tHeaders["name"]] # text
end if
end if
put offset(tBoundary, tData) into tOffset
end repeat
--
if tRequestArray["image"]["filename"] is not "" then
put UploadBaseDir_images into UploadBaseDir
put toLower(tRequestArray["image"]["filename"]) into tFile
set itemDel to "."
put item -1 of tFile into theExt
delete item -1 of tFile
put tFile & "." & theExt into tFileName
replace space with "" in tFileName
put tRequestArray["image"]["binData"] into URL ("binfile:" & uploadBaseDir & tFileName)
put "Upload OK"
else if tRequestArray["application"]["filename"] is not "" then
put UploadBaseDir_apps into UploadBaseDir
put toLower(tRequestArray["application"]["filename"]) into tFile
set itemDel to "."
put item -1 of tFile into tExt
delete item -1 of tFile
set itemDel to comma
if tRequestArray["Catalog"] is not ""
then put "/product_catalogs/" & tFile & "." & tExt into tFileName
else if tRequestArray["LookBook"] is not ""
then put "/lookbook/" & tFile & "." & tExt into tFileName
set itemdel to "/"
replace space with "" in tFileName
if tRequestArray["Catalog"] is not ""
then put tRequestArray["application"]["binData"] into URL ("binfile:" & char 1 to -2 of uploadBaseDir & tFileName)
else if tRequestArray["LookBook"] is not ""
then put tRequestArray["application"]["binData"] into URL ("binfile:" & char 1 to -2 of uploadBaseDir & tFileName)
else put tRequestArray["application"]["binData"] into URL ("binfile:" & char 1 to -2 of uploadBaseDir & withUnderScoreToMinus(tFileName))
put "Upload OK"
end if
else put pseudo404() ### 9 ###
end rias_upl
Happy new year to you and yours,

Pierre
Hello everyone,
The LiveCode website has a tutorial on uploading files to a server with LiveCode Server. Anyone know how to specify the folder on the server where files are to be uploaded? It appears that the default is tmp.
Regards,
Gregory
_______________________________________________
use-livecode mailing list
http://lists.runrev.com/mailman/listinfo/use-livecode
--
Pierre Sahores
mobile : 06 03 95 77 70
www.sahores-conseil.com
Gregory Lypny
2013-12-29 19:26:51 UTC
Permalink
Hi Pierre,

Thank you kindly sharing your code. I’m afraid that it is a little too advance for my skills, and my needs are much simpler. I am able to create an upload form and move the uploaded file from the tmp folder on my Mac to another folder using a shell command. There are only two problems left that I need to fix. One is that an internal server error occurs (error 500) and it is output to the web page. The other problem is that the moved file has read and write access for group _WWW but no access for anyone else.

Here is what I did. The HTML in the web page is

<html>
<head></head>
<body>
<H1>Upload Form</H1>
<form enctype="multipart/form-data" action="submitAssignmentScript.lc" method="POST">
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>

where the action, “submitAssignmentScript.lc”, simply refers to a handler named submitAssignment in a stack named myServer

<?lc
start using stack myServer.livecode
submitAssignment
?>

The handler contains the following

on submitAssignment
put the keys of $_Files[uploadedfile] into theKeys
put the long date && long time & return into output
repeat for each line thisLine in theKeys
put thisLine & ":" && $_Files[uploadedfile][thisLine] & return after output
end repeat
delete the last character of output
put output into url (dataFilePath & "Upload Info.csv")
get shell ("mv" && $_Files[uploadedfile][filename] && "../share/httpd/myData/" & “Uploaded-File.pdf")
end submitAssignment

where “Upload Info.csv” contains some information from $_Files.

If anyone can help me figure out why I’m getting the the internal server error and how I can change the moved file’s permissions, I’d much appreciate it.

Regards,

Gregory
Pierre Sahores
2013-12-29 20:29:05 UTC
Permalink
Hi Gregory,

Sorry. As i never went in this way to design files upload procedures, i will not be the more able to help to get this up and running. Perhaps should you follow the LiveCode example you spoken about yesterday. Comparing the way it works under PHP could help too ;_/

Note : you would normally not have to handle the file move by shell : it works out of the box on livecode server using :

put url ("binfile://pathtocurrentfiledestination.pdf") into url ("binfile://pathtonewfiledestination.pdf")

Pierre
Objet : Rép : Uploading Files with LiveCode Server
Date : 29 décembre 2013 20:26:51 HNEC
Hi Pierre,
Thank you kindly sharing your code. I’m afraid that it is a little too advance for my skills, and my needs are much simpler. I am able to create an upload form and move the uploaded file from the tmp folder on my Mac to another folder using a shell command. There are only two problems left that I need to fix. One is that an internal server error occurs (error 500) and it is output to the web page. The other problem is that the moved file has read and write access for group _WWW but no access for anyone else.
Here is what I did. The HTML in the web page is
<html>
<head></head>
<body>
<H1>Upload Form</H1>
<form enctype="multipart/form-data" action="submitAssignmentScript.lc" method="POST">
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
where the action, “submitAssignmentScript.lc”, simply refers to a handler named submitAssignment in a stack named myServer
<?lc
start using stack myServer.livecode
submitAssignment
?>
The handler contains the following
on submitAssignment
put the keys of $_Files[uploadedfile] into theKeys
put the long date && long time & return into output
repeat for each line thisLine in theKeys
put thisLine & ":" && $_Files[uploadedfile][thisLine] & return after output
end repeat
delete the last character of output
put output into url (dataFilePath & "Upload Info.csv")
get shell ("mv" && $_Files[uploadedfile][filename] && "../share/httpd/myData/" & “Uploaded-File.pdf")
end submitAssignment
where “Upload Info.csv” contains some information from $_Files.
If anyone can help me figure out why I’m getting the the internal server error and how I can change the moved file’s permissions, I’d much appreciate it.
Regards,
Gregory
_______________________________________________
use-livecode mailing list
http://lists.runrev.com/mailman/listinfo/use-livecode
--
Pierre Sahores
mobile : 06 03 95 77 70
www.sahores-conseil.com
Alex Tweedly
2013-12-30 23:38:43 UTC
Permalink
I think you're getting the error 500 because your script doesn't produce
any output.

Just adding a line something like
put "File uploaded OK"

near the end should be enough

(or of course, detect any errors if they do occur, include the file name
in the message, ...)

-- Alex.
Post by Gregory Lypny
Hi Pierre,
Thank you kindly sharing your code. I’m afraid that it is a little too advance for my skills, and my needs are much simpler. I am able to create an upload form and move the uploaded file from the tmp folder on my Mac to another folder using a shell command. There are only two problems left that I need to fix. One is that an internal server error occurs (error 500) and it is output to the web page. The other problem is that the moved file has read and write access for group _WWW but no access for anyone else.
Here is what I did. The HTML in the web page is
<html>
<head></head>
<body>
<H1>Upload Form</H1>
<form enctype="multipart/form-data" action="submitAssignmentScript.lc" method="POST">
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
where the action, “submitAssignmentScript.lc”, simply refers to a handler named submitAssignment in a stack named myServer
<?lc
start using stack myServer.livecode
submitAssignment
?>
The handler contains the following
on submitAssignment
put the keys of $_Files[uploadedfile] into theKeys
put the long date && long time & return into output
repeat for each line thisLine in theKeys
put thisLine & ":" && $_Files[uploadedfile][thisLine] & return after output
end repeat
delete the last character of output
put output into url (dataFilePath & "Upload Info.csv")
get shell ("mv" && $_Files[uploadedfile][filename] && "../share/httpd/myData/" & “Uploaded-File.pdf")
end submitAssignment
where “Upload Info.csv” contains some information from $_Files.
If anyone can help me figure out why I’m getting the the internal server error and how I can change the moved file’s permissions, I’d much appreciate it.
Regards,
Gregory
_______________________________________________
use-livecode mailing list
http://lists.runrev.com/mailman/listinfo/use-livecode
Continue reading on narkive:
Loading...