Uploading Files with LiveCode Server
Pierre Sahores
sc at sahores-conseil.com
Sat Dec 28 21:10:38 EST 2013
Hi Gregory,
Out of a production app.
The client side code :
> 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
The server side code :
> 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
Le 29 déc. 2013 à 02:01, Gregory Lypny a écrit :
> 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
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
--
Pierre Sahores
mobile : 06 03 95 77 70
www.sahores-conseil.com
More information about the use-livecode
mailing list