Advertisement
Guest User

Untitled

a guest
May 4th, 2017
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. -- uploadFileByFTP
  2. -- uploads a file by FTP (non-secure/sFTP!)
  3. --
  4. -- usage:
  5. -- uploadFileByFTP "ftp://ftp.domain.com/" "c:\\temp\\file.ext"
  6. -- -- uploads to /file.ext
  7. -- uploadFileByFTP "ftp.domain.com" "c:\\temp\\file.ext"
  8. -- -- uploads to /file.ext
  9. -- uploadFileByFTP "ftp.domain.com/folder/" "c:\\temp\\file.ext"
  10. -- -- uploads to /folder/file.ext . Trailing slash is very important!
  11. --
  12. -- uploadFileByFTP "ftp.domain.com/folder/otherfile.ext" "c:\\temp\\file.ext"
  13. -- -- uploads file as /folder/otherfile.ext
  14. -- uploadFileByFTP "ftp.domain.com/folder" "c:\\temp\\file.ext"
  15. -- -- uploads file as /folder (filename - told you the trailing slash is very important
  16. --
  17. -- uploadFileByFTP "ftp.domain.com" "c:\\temp\\file.ext" user:"myusername" pass:"password"
  18. -- -- uploads file as /file.ext , logging in with given username and password
  19. --
  20. -- uploadFileByFTP "ftp.domain.com" "c:\\temp\\file.ext" err:&myVar
  21. -- -- uploads file as /file.ext , stores error (if any) in myVar
  22. --
  23. -- returns:
  24. -- true -- upload successful
  25. -- false -- upload failed. Pass a by-reference variable to err: to retrieve the error.
  26. fn uploadFileByFTP FTPpath file &err: user: pass: = (
  27. local FTP = dotNetObject "System.Net.WebClient"
  28.  
  29. local pathChunks = filterString FTPpath "/"
  30.  
  31. local FTPdomain
  32. if (pathChunks[1] != "ftp:") do ( insertItem "ftp:" pathChunks 1 )
  33. FTPdomain = pathChunks[2]
  34. FTP.baseAddress = "ftp://" + FTPdomain + "/"
  35.  
  36. local FTPtarget = ""
  37. if (pathChunks[3] != undefined) then (
  38. for i = 3 to (pathChunks.count - 1) do (
  39. append FTPtarget (pathChunks[i] + "/" )
  40. )
  41. append FTPtarget pathChunks[pathChunks.count]
  42. if (FTPpath[FTPpath.count] == "/") then ( append FTPtarget ("/" + (filenameFromPath file)) )
  43. )
  44. else (
  45. FTPtarget = filenameFromPath file
  46. )
  47.  
  48. FTP.credentials = (dotNetObject "System.Net.NetworkCredential")
  49. FTP.credentials.username = if (user != unsupplied) then ( user ) else ( "anonymous" )
  50. FTP.credentials.password = if (pass != unsupplied) then ( pass ) else ( "ano@nym.us" )
  51.  
  52. local returnval = true
  53. try ( FTP.uploadFile FTPtarget file)
  54. catch ( returnval = false; err = getCurrentException() )
  55. FTP.dispose()
  56. return returnval
  57. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement