uploading file with jsftp (or other module)

721 views
Skip to first unread message

Brendan Thompson

unread,
Nov 14, 2014, 2:23:34 AM11/14/14
to nod...@googlegroups.com
Hello. I'm developing an extension for Adobe CC products (CEP) that uses node to upload a small CSV file to an ftp server.

Using jsftp I'm able to write a file to my server but the contents are empty. Its just and empty file and not the data I intended to send.

Here is my code.

function uploadHours() {
    var jsftp = require("jsftp");

var ftp = new jsftp({
  host: localStorage.host,
  port: Number(localStorage.port), // defaults to 21
  user: localStorage.user, // defaults to "anonymous"
  pass: localStorage.ftpPass // defaults to "@anonymous"
});

ftp.auth(localStorage.user, localStorage.ftpPass, function(hadErr) {
    if (!hadErr)
    alert("auth succesfull")
});
    ;
  ftp.put('C:/Program Files/Adobe/Adobe After Effects CC 2014/Support Files/Hours Tracker/index2.html', '/public_html/indexTester2.html', function(hadErr) {
      if (!hadError)
    alert("File transferred successfully!");
  });
}

I'm what you call a total rookie at node so any help is awesome.

Ryan Schmidt

unread,
Nov 14, 2014, 5:36:03 AM11/14/14
to nod...@googlegroups.com
Is this code running in a browser or in node? If it's running in a browser, then I don't see how it can work, because you're accessing filesystem paths ("C:/Program Files/...") which you won't have permission to do. And if it's running in node, then I don't see how it can work, because you're accessing the nonexistent alert function and localStorage object; try using console.log instead of alert, and using normal variables instead of localStorage.

Either way, there are a lot of things in this code that look wrong.

I'm not familiar with jsftp, but I'm confused by the fact that you're passing the username and password in the options object when calling the jsftp constructor, but then are passing the same username and password to the ftp.auth function. Seems odd that you would have to provide the credentials twice. Are you sure that's how jsftp is meant to be used?

Your code doesn't look very asynchronous to me. You're calling ftp.auth, and not waiting for the result of that operation before calling ftp.put. Possibly the server hasn't authenticated you yet by the time you've started the put. For starters, try putting the ftp.put invocation inside the callback in the ftp.auth invocation, after you've ensured no error occurs. You also need a way of informing whatever code called uploadHours() when it is complete, and if an error occurred. Usually this is done by using a callback, i.e. you would define your function as "function uploadHours(callback)" and it would call "callback()" when it's done, or "callback(err)" if an error occurred (assuming "err" is an error object describing the error).

Note also that you have a typo in your error handling. In your ftp.put invocation you've named the parameter "hadErr" but are then accessing the undefined variable "hadError". Looks like the jsftp project's documentation uses these two different variable names in different examples, which is confusing. You might send them a pull request to always use the same error variable name for consistency.



Reply all
Reply to author
Forward
0 new messages