Save current time to a file . txt

Asked

Viewed 657 times

1

How to make an online post of a variable in a given TXT? I tried the following, but it didn’t work:

function post(theUrl)
{
    var xmlHttp = null;

    xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "POST", theUrl, false );
    xmlHttp.send("Hora Atual" + $hora);
    return xmlHttp.responseText;
}

post("http://www.example.com/aviso.txt");

3 answers

5

  • Are you sure @Silvio Andorinha ? Because I have another function in JAVA SCRIPT where I look for a line inside a hosted txt. It works perfectly.

  • 1

    Maybe it’s just another POG.

  • 2

    What @Silvioandorinha meant is that it is not possible to recover "POST" variables from a txt file you will need a web server running some server technology-side after that you can even for example as in apache configure which txt files will run PHP but tbm see no need for it.

  • 1

    @user7605 here

5

If what you are trying to do is generate a . txt the answer is: Yes, it is possible.

HTML5 allows the use of the attribute download in tags <a> that does nothing more than force the contents of that link to be downloaded. Example:

HTML:

<a href="#" download="data.txt" id="download">Download da data</a>

Javascript:

var mt, data, download;
mt = 'data:text/plain;charset=utf-8,';
data = new Date();
download = mt + encodeURIComponent(data);

document.getElementById('download').setAttribute('href',download);

Exemplo

More about the attribute download:

  • Can be used to download client-side generated images via base64;
  • Do not overwrite HTTP headers (if a header directive enters confilto with it, the header prevails);
  • actually I do not need to download, I need to send a POST with the current date through Javascript to a TXT hosted on a server, through HTTP...understand ?

1

For your very brief explanation, what I could understand is that you want to simply record a certain timestamp in a text file.

If you first thought of AJAX, you should at least keep in mind that there needs to be a server-side application (PHP, ASP, Python...) to perform the manipulation of the filesystem, effectively creating that file.

So, what’s the point of going all the way around, involving Javascript, obstructing your application, if a simple link pointing to a particular URL responsible for creating the file is enough? An example with PHP, since it is popular and I do not know what your server language:

<a href="/path/to/my/resource.php">Gerar Aquivo</a>

<?php file_put_contents( 'aviso.txt', date( 'd/m/Y - H:i:s' ) . "\n", FILE_APPEND );

Aside from checking write permissions, I solved a specific problem in two lines, one of HTML and one of PHP.

If I’m wrong, edit your answer and provide more details. Help us help you.

Browser other questions tagged

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