Uploading file to LC Server from desktop app

Are you using LiveCode to create server scripts or CGIs?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Uploading file to LC Server from desktop app

Post by bangkok » Tue Jun 18, 2013 9:14 am

I've managed to upload files from a lc server script with a form, following this method :

http://lessons.runrev.com/s/lessons/m/4 ... ode-server

But now, I would like to upload a file from a desktop stack.

I guess it's necessary to build a special header before posting the file, to the lc script ? Someone could give me a clue ?

I tried this script, but without success :
http://forums.runrev.com/viewtopic.php?f=49&t=12954

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Uploading file to LC Server from desktop app

Post by bangkok » Wed Jun 19, 2013 8:04 am

OK I found a solution, using libUrlMultipartFormData, thanks to :
http://www.lacscentre.com/liburl/liburl ... rtFormData

Here is the LiveCode script :

Code: Select all

on mouseUp
   put empty into tForm
   put "http://www.my.com/yourcgi.lc" into tURL
   
   answer file "Choose file: "
   if it is empty then exit mouseUp
   put "<file>" & it into tFile 
   
   --- note the "file". The same name must be used with the $_FILES var in LC server script
   if libUrlMultipartFormData (tForm,"file", tFile) is not empty then
      answer it 
   else
      --- success
      set the httpHeaders to line 1 of tForm
      post line 2 to -1 of tForm to url tUrl 
      answer it
      set the httpHeaders to empty
   end if
   
end mouseUp
And now the LC Server script :

Code: Select all

<?lc

set the errormode to "inline"

if $_FILES["file"]["error"] then
   put "ERROR"
else
   put $_FILES["file"]["name"] into tFileName
    put $_FILES["file"]["type"] into tFileType
    put $_FILES["file"]["size"] into tFileSize
    put $_FILES["file"]["filename"] into tFilePath
    put tFilePath&" = "&"Your file '" & tFileName & "' was uploaded successfully. It is" && tFileSize && "bytes in size."
    
set the defaultFolder to "test/"

### if the file is a text file, use "file" instead of "binfile"
put URL ("binfile:" & tFilePath) into url ("binfile:"&tFileName )

put the result
    
end if    

?>
However I'm still looking for a working script using POST instead of libUrlMultipartFormData .

Post Reply

Return to “CGIs and the Server”