9

I dont understand why cant javascript make ftp calls?. Why do we have to make such a request using server?

Even Browsers have ability to authenticate and browse a ftp server. Maybe use browser api's to do it?

iamkhush
  • 2,562
  • 3
  • 20
  • 34
  • have you looked into this question? http://stackoverflow.com/questions/5338444/is-it-possible-to-download-file-from-ftp-using-javascript – Shishya Nov 21 '14 at 16:51
  • Yes, but I know about window.open. My question is relating to JS network calls for FTP operations. – iamkhush Nov 21 '14 at 20:55
  • have you checked fireftp? https://github.com/mimecuvalo/fireftp – Ammar Nov 23 '14 at 17:31
  • Yes, just did. It looks like a firefox plugin. Although it uses js files underneath, Im not sure how does it work, because its for Firefox only. So even if I consider this, any reason for my question `why cant javascript make ftp calls ?` I mean, we can do http calls using it.Ajax? – iamkhush Nov 23 '14 at 23:21
  • This is actually a very interesting question but should likely be re-worded. – JSON Nov 24 '14 at 19:15
  • 1
    @JSON Please go ahead and edit it if you feel so. – iamkhush Nov 24 '14 at 19:24

4 Answers4

9

Ok, answering my own question here.

I went through Mozilla docs on XMLHTTPRequest. It specifically says -

Despite its name, XMLHttpRequest can be used to retrieve any type of data, not just XML, and it supports protocols other than HTTP (including file and ftp).

So, I am satisfied with this. JavaScript can make calls to ftp using this.

iamkhush
  • 2,562
  • 3
  • 20
  • 34
  • 2
    Yes, but there doesn't seem to be anyone who has written the code to do it. – Eddie Jan 25 '18 at 18:32
  • Yes, and I guess the reasons can be read at the `Differences from HTTP` section on https://en.wikipedia.org/wiki/File_Transfer_Protocol – iamkhush Oct 17 '19 at 10:56
  • It would be magic to have full ftp in JS. It's a big task though. ipv4, ipv6, sftp, line endings, encoding, multiple protocol versions, file permissions. It's a complex protocol and I don't think it's possible unless there is a financial incentive – Eddie Oct 19 '19 at 01:54
  • It won't help much in the browser, if you use Nodejs as a server though, maybe the scp2 module could work for your needs. – Eddie Oct 19 '19 at 02:06
  • 2
    @Eddie What's avoiding such usage is same origin policy: XMLHttpRequest of http://whatever.thing cannot access to ftp://whatever.thing because the origin differ (schemes differ), and since ftp scheme is not HTTP, you have no way AFAIK to tell the browser the CORS is allowed. To make JS FTP-friendly, one would need to request FTP standard to allow CORS "headers" or so. – Xenos Nov 27 '19 at 14:05
  • 2
    The mozilla docs have changed to specifically remove the part about "other protocols". Now just says `Despite its name, XMLHttpRequest can be used to retrieve any type of data, not just XML.` – Roddy May 01 '20 at 17:49
2

The title of this question suggests the requester is keen on understanding if an FTP transfer could be implemented using JavaScript. However looking at the the answer by the same requester it appears that the question was just to know if URLs with FTP protocols can be used with JS and possibly HTML tags. The answer is yes. Even a simple <a> tag supports FTP URLs in the href attribute. Hope this helps the new readers. And yes, the XMLHttpRequest AJAX object does enable calling a URL with an FTP protocol.

Cheers.

Kartik
  • 93
  • 6
2

There is a JavaScript library at http://ftp.apixml.net/ that allows FTP file uploads via JavaScript.

In this case, technically, the ftpjs server is making the FTP connection to the FTP server, but the instructions are being passed via JavaScript. So this particular library is designed primarily to allow developers add a basic file upload mechanism without writing sever-side code.

Under the hood, it uses the HTML5 FileReader to read the file to a base64 string, and then posts this using CORS AJAX back to the server.

// Script from http://ftp.apixml.net/
// Copyright 2017 http://ftp.apixml.net/, DO NOT REMOVE THIS COPYRIGHT NOTICE
var Ftp = {
    createCORSRequest: function (method, url) {
        var xhr = new XMLHttpRequest();
        if ("withCredentials" in xhr) {
            // Check if the XMLHttpRequest object has a "withCredentials" property.
            // "withCredentials" only exists on XMLHTTPRequest2 objects.
            xhr.open(method, url, true);
        } else if (typeof XDomainRequest != "undefined") {
            // Otherwise, check if XDomainRequest.
            // XDomainRequest only exists in IE, and is IE's way of making CORS requests.
            xhr = new XDomainRequest();
            xhr.open(method, url);
        } else {
            // Otherwise, CORS is not supported by the browser.
            xhr = null;
        }
        return xhr;
    },
    upload: function(token, files) {
        var file = files[0];
        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.addEventListener("load",
            function() {
                var base64 = this.result;               
                var xhr = Ftp.createCORSRequest('POST', "http://ftp.apixml.net/upload.aspx");
                if (!xhr) {
                    throw new Error('CORS not supported');
                }
                xhr.onreadystatechange = function() {
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        Ftp.callback(file);
                    }
                };
                xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                xhr.send("token=" + token + "&data=" + encodeURIComponent(base64) + "&file=" + file.name);
            },
            false);
    },
    callback: function(){}
};
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Fiach Reid
  • 6,149
  • 2
  • 30
  • 34
  • It has validation issues while generating token. – SBimochan Jan 14 '20 at 14:22
  • 2
    This Site asks you for all your credentials for generating the token. When you click generate it sends all your data in cleartext and over http to an backend: http://ftp.apixml.net/StoreFtp.aspx?&Host=ftp.fuckyou.com&Username=you@asshole.com&Password=youresostupid&Path=/var/www/pictures&Mode=FTP Don't use this and if you did change your data now ! – R. Keller Jan 20 '20 at 08:31
0

its very difficult to FTP data(BIGfile) to backup server without using HTTP protocol in a web application.

Lets say, S1-( Client Browser ), S2-(code container server), S3-(Files Backup server) and we want to upload 2gb file from s1 using FTP.

use case diagram

enter image description here

This can be done by "JavaApplet" . we can embed uploader applet in webapplication. This applet will run inside browser sand box.
go through link

sample code for ftp using applet

provided you have to enable java on your browser.

coder
  • 8,346
  • 16
  • 39
  • 53