What Way XPG FTP Access Via Javascript

Asked

Viewed 614 times

0

You can access my free account on the XPG hosting server, through Javascript?

I wonder if it is possible to make a connection with Javascript only with an FTP server(ftp://ftp.xpg.com.br), to "Send" files.

I need somehow while selecting the local file to already log in to FTP(ftp://ftp.xpg.com.br) then do "upload".

I know it does, with the advances of Ecmascript, Apis, DOM It already does some wonders that in the past was not possible, of the type to access files of the Computer.

Fortunately, this does not happen anymore, thanks to the Filesystem API. With the Filesystem API, a web application can create, read, browse and write to a sandbox section of the user’s local file system.

Node.js is a platform for developing network-based server-side applications using Javascript and the V8 Javascript Engine, that is, with Node.js we can create a variety of web applications using Javascript code only.

But my knowledge on Javascript subject and Libraries is particularly reasonable. That’s a polite way of saying don’t know where to start.

I found navigating inside Sopt - Upload a file with AJAX this is a quote on part of the subject mentioned here "Sending files"

It remains to be seen how log in to the account.

  • I’ve never tried, but I think with ajax it might be possible. See that there’s an excerpt that says "(including file and ftp)" in here.

1 answer

1


Searching, I found a javascript API for such: http://www.ftpjs.xyz/.

With it it is possible to send input files:

<script src="http://ftpjs.xyz/ftp.js"></script>

<input type=file onchange="Ftp.upload('access_token', this.files)"/>

The site itself generates this token based on the connection data you provide.

I found it very simple and cool despite being just upload!


Then follow the code to know if it is feasible or not to use:

// Script from http://FTPJS.XYZ
// Copyright 2016 FTPJS.XYZ, 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://www.ftpjs.xyz/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(){}
};
  • 2

    I don’t know if you understand, but it doesn’t connection to FTP using Javascript - I find it very unlikely that this is possible (speaking of client-side, Node is likely to exist something). This is just a lib that makes requests to a server and there are the requests pro FTP. So be careful, because you don’t know whether or not they are saving your FTP server credentials somewhere. I am not doubting the goodwill of the lib developers, but it seemed to me that you did not realize this, so I thought it best to warn you =).

  • There’s an ASPX page in the backend.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.