Created
November 7, 2014 15:29
-
-
Save patcullen/1720d5fbf9c3b9b8d8f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Language=JScript %> | |
<!--#include file="json.asp" --><%/* https://raw.githubusercontent.com/douglascrockford/JSON-js/master/json2.js */%> | |
<% | |
var ftp = (function() { | |
/* | |
* Copy a file(s) to a directory on a remote FTP server. | |
* | |
* Adapted from the very usefull post @ http://benmeg.com/code/asp/ftp.asp.html | |
*/ | |
function copyTo(address, username, password, remote_directory, files_to_put, isBinary) { | |
var objFSO = Server.CreateObject("Scripting.FileSystemObject"), | |
oScript = Server.CreateObject("WSCRIPT.SHELL"), | |
oFileSys = Server.CreateObject("Scripting.FileSystemObject"), | |
objTextFile, oScriptNet, oFile, strCMD, strTempFile, | |
strCommandResult = '', | |
uniqueNumber = '__'; // This is for you to implement a random key on your system (if required). | |
// Build our ftp-commands file | |
objTextFile = objFSO.CreateTextFile(Server.MapPath('ftpCommand' + uniqueNumber + '.ftp')); | |
objTextFile.WriteLine('lcd ' + Server.MapPath('.')); | |
objTextFile.WriteLine('open ' + address); | |
objTextFile.WriteLine(username); | |
objTextFile.WriteLine(password); | |
// Check to see if we need to issue a 'cd' command | |
if (remote_directory != '') | |
objTextFile.WriteLine('cd ' + remote_directory); | |
objTextFile.WriteLine('prompt'); | |
// If the file(s) is/are binary (i.e. .jpg, .mdb, etc..) | |
if (isBinary) | |
objTextFile.WriteLine('binary'); | |
// If there are multiple files to put, we need to use the command 'mput', instead of 'put' | |
if (files_to_put.indexOf('*') > -1) | |
objTextFile.WriteLine('mput ' + files_to_put); | |
else | |
objTextFile.WriteLine('put ' + files_to_put); | |
objTextFile.WriteLine('bye'); | |
objTextFile.Close(); | |
delete objTextFile; | |
// Pipe output from cmd.exe to a temporary file | |
strTempFile = Server.MapPath('ftpOutput-' + uniqueNumber + '.ftp'); | |
// Use cmd.exe to run ftp.exe, parsing our newly created command file | |
oScript.Run('cmd.exe /c ' + 'ftp.exe -s:' + Server.MapPath('ftpCommand' + uniqueNumber + '.ftp') + ' > ' + strTempFile, 0, true); | |
oFile = oFileSys.OpenTextFile(strTempFile, 1, false, 0); | |
// Grab output from temporary file | |
strCommandResult = oFile.ReadAll(); | |
oFile.Close(); | |
// Delete the temporary & ftp-command files | |
oFileSys.DeleteFile(strTempFile, true); | |
objFSO.DeleteFile(Server.MapPath('ftpCommand' + uniqueNumber + '.ftp'), true); | |
delete oFileSys; | |
delete objFSO; | |
return { | |
result: strCommandResult.split('\r\n') | |
}; | |
} | |
return { | |
copyTo: copyTo | |
}; | |
})(); | |
Response.write( | |
JSON.stringify( | |
ftp.copyTo( | |
'some.host.com', 'some_user', 'some_password', | |
'remote_directory', | |
'c:/some/folder/and/file.txt' | |
) | |
) | |
); | |
%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment