0

I had just posted a recent question using client-ftp and was told that package was outdated and was the source of my original problem. As suggested I moved to an package that is still being maintained, which is jsftp. However, I am still having similiar issues. For reference my previous question can be found here. I will still restate my problem as it has changed slightly.

I am new to Angular/Node and am trying to connect to an ftp server.

I am creating a button on the front end like this: <button class="btn btn-primary" (click)="downloadFile()"><i class="fa fa-file-photo-o"></i> Download Screenshot</button>

My new implementation to download is this:

downloadFile(){ console.log('Connecting to sftp...'); var jsftp = require("jsftp"); var ftp = new jsftp({ host: 'localhost', port: 22, user: 'anonymous', pass: 'anonymous' }); console.log('Downloading Screenshot...'); } 

I realize that I'm not trying to download anything, but for right now I'm only trying to connect first. The problem is that when the click event is fired I get an error saying TypeError: createConnection is not a function. I have no idea what I'm doing wrong. The createConnection function isn't being called anywhere. I'm doing what the documentaion says and other resources seem to indicate the same, such as here.

Node version is: v8.11.3 - maybe that is too outdated?

1
  • Browsing through some posts like this github.com/browserify/browserify/issues/1780 I guess that JS based FTP-Clients are not supposed to run in a browser. You have to build up your own server and implement the client there. Only then you can access the Net module which is not reachable out of the browser and causes the error you mention. This seems to be a general issue. Commented Aug 2, 2018 at 19:55

1 Answer 1

1

You tagged typescript, presumably because of the TypeError, which is a javascript, not a typescript, error (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_a_function).

Secondly, you seem to be confused between javascript on NodeJS, which is the back end, and in the browser, which is the front end. You cannot run jsftp in the browser, so your approach needs to change.

If your intent is to let the user download an ftp file from their browser when they click the link, you should be able to link to it directly in the href, without any javascript. Take a look at the answers to this question for more info: Is it possible to download file from FTP using Javascript?

Sign up to request clarification or add additional context in comments.

2 Comments

Wouldn't adding a link to the ftp on the front end like that be a potential security risk?
That depends. If the FTP server allows anonymous downloads, then you're not risking anything. On the other hand, providing username and password directly in the link would most likely be a pretty bad idea. If the FTP server is secured, but you need to allow downloads anyway, then you'll need a more complex setup that involves proxying requests on the server.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.