0

I am developing a self-contained (stand-alone) HTML form. In one part of the form, I would like to make it possible to select the name of a file that is on an FTP.

In my .html file, I am therefore trying to find a way to list the names of all the files on an FTP.

Based on what I have read so far, it seems there is no conventional way to do this as accessing an FTP should be done using server-side languages such as PHP for example, as explained here or here.

So I am trying to see if I can do this in a different way. An idea I had is to copy the content displayed on an FTP page into a JavaScript variable, as shown here.

I am using as an example the FTP from the German Climate Data Center because it is an open access FTP that doesn’t require any login information. In the code below, I am trying to place the content displayed on a page of that FTP into a <div> inside my .html file, and then display the <div> using console.log. However, as shown below, I haven’t been successful so far:

function start_FTP() {
  var server = "ftp-cdc.dwd.de/test/weather/radar/sites/sweep_vol_z/mem/hdf5/filter_polarimetric/";
  var ftpsite = "ftp://" + server;
  window.open(ftpsite);

  document.getElementById("FTP_content").innerHTML = '<object type="text/html" data=ftpsite ></object>';
  console.log(FTP_content);
}
<h2> List FTP content using JavaScript </h2>
<button type="button" id="FTP_button" name="FTP_button" onclick="start_FTP()">Select File Name</button>
<div id="FTP_content"> </div>

The <div> does not contain any useful information.

This is just a step towards my final goal, which is in fact to create a new window that lists the contents on the FTP page with a checkbox next to each line, such that the user can select the name of the file he needs.

Is this possible using JavaScript? If not, are there any other “tricks” that can be used to reach this goal using JavaScript only?

EDIT

The issue I have is that I can not do anything on the server-side (unfortunately).

I need to develop a stand-alone HTML form that needs to be sent to lots of different end users, who need to be able to use this form “as is”. These end users (clients) need to be able to select the name of a file from a list of files that can be seen on an open access FTP.

So I am trying to copy the names of the files that are visible on an open access FTP page, and add a check box in front of each file name, such that the end user can select the check box that corresponds to the name of the file (that will be used for further data-processing).

Percy
  • 177
  • 1
  • 6
  • 1
    I don't think it's possible to do most FTP functions in a webpage without a server being the middleman – TKoL Dec 31 '20 at 12:55
  • 1
    Will you consider using PHP ? – Ken Lee Dec 31 '20 at 13:25
  • The issue I have is that I can not do anything on the server side. I need to develop a stand-alone html form to send to different end users, and these end users need to be able to select the name of a file that they need, among a list of files uploaded to an FTP. – Percy Jan 01 '21 at 13:56
  • @Ken Lee From what I understand, it is not possible to run PHP from a stand-alone HTML file (that needs to be sent to lots of different end users, who need to be able to use it “as is”). Of course, if there is a language that’s compatible I would be happy to use it but so far I don’t think there is one. For these reasons, I have been trying to copy the content of what is shown on an FTP page. – Percy Jan 01 '21 at 13:56
  • That's why I asked -- will you consider using PHP instead of a stand-alone HTML. (may be my previous reply is too short, sorry). But if you use PHP it is expected that you will send the PHP URL to your users with login username and password so they can access the files thru a browser. – Ken Lee Jan 01 '21 at 13:59
  • A constraint I have is that the form must be compatible with any web browser (without the need for any add-ons or extra configurations to be done by the end user), so I don’t think PHP is an option. (As the form will be sent to lots of different end users, it needs to be easily usable “as is”.) In part of the form, the end user just needs to select the name of a file on an open access FTP (no username or password required), like the FTP in the code I provided above. – Percy Jan 01 '21 at 14:34

1 Answers1

0

Do you Mean something like this? It requires Jquery.

Javascript,css and then the HTML.

function handleFileSelect (e) {
    var files = e.target.files;
    if (files.length < 1) {
        alert('select a file...');
        return;
    }
    var file = files[0];
    var reader = new FileReader();
    reader.onload = onFileLoaded;
    reader.readAsDataURL(file);
}

function onFileLoaded (e) {
    var match = /^data:(.*);base64,(.*)$/.exec(e.target.result);
    if (match == null) {
        throw 'Could not parse result'; // should not happen
    }
    var mimeType = match[1];
    var content = match[2];
    alert(mimeType);
    alert(content);
}

$(function () {
    $('#import-pfx-button').click(function(e) {
        $('#file-input').click();
    });
    $('#file-input').change(handleFileSelect);
});
#file-input {
    display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button" id="import-pfx-button">Browse...</button>
<input type="file" id="file-input" />
<output id="list"></output>
nirualjs
  • 33
  • 5
  • No, not really: I would like to access an FTP, for example the one provided in my code snippet above; URL: ftp://ftp-cdc.dwd.de/test/weather/radar/sites/sweep_vol_z/mem/hdf5/filter_polarimetric/ I would like the names of the files listed on that FTP page to be listed in a new window with a checkbox next to each so that the user can select which file he needs. The name of that file will then be inserted into the rest of the form. – Percy Jan 01 '21 at 16:34
  • I don’t really understand what your code does – I don’t know jquery well, but when I run your code, it prompts me to select a file on my computer then seems to display random characters. Can it be adapted to JS and list the file names in the FTP link above? – Percy Jan 01 '21 at 16:36