3

For some reason I got stuck with some events on jQuery/JS

function update()
{
    if(scrolling == true) {
        return;
    }
    var count = 0;
    jQuery.get('count.txt', function(data) {
        count = data;
    }).done(function() {
        var countstr = '' + count;
        myImage.src = "latest" + countstr + ".jpg#" + new Date().getTime();
        setTimeout(update, 1000);
    });
}

In my last question I asked about the jQuery "done function"

Currently I am working with a Timeout/timer to update the image every second

setTimeout(update, 1000);

It does work but I know that this can't be the smartest solution. In C# I'm able to use a FileWatcher to use an event to check if there is a new file in the folder

FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = path;
            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
               | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            watcher.Filter = "*.jpg";

            watcher.Created += new FileSystemEventHandler(OnChanged);

            watcher.EnableRaisingEvents = true;

Is there an API or an event for jQuery/JS to check that? I was also looking to work with AJAX but I got no experiences with AJAX.

//edit I know that JS is not able to do that. But I was just wondering if there is another way to use this event (like AJAX or Node.js)

What Am I doing? I made a software which will upload many images on my ftp server. images0, images1, images2 etc.

The event should check if there was another image uploaded and should show this instead of the old image

Florian Zaskoku
  • 477
  • 3
  • 13
  • JS does not have any access to the local file system of the client machine - which is a very good thing. What are you trying to achieve with this code as I'm certain there's a better way – Rory McCrossan Oct 26 '17 at 12:58
  • 1
    Obviously the browser's JS code can't watch for a file directly because the file is on the server and it has no access to it. This sounds potentially like a candidate for a solution involving websockets / SignalR – ADyson Oct 26 '17 at 12:59
  • 1
    Thanks. I know that JS is not able to do that. I updated my question – Florian Zaskoku Oct 26 '17 at 13:01

2 Answers2

2

Florian, as it was already mentioned, you cannot do it with client JS code.

What would I use in this case (I assume it's the universal solution):

  1. NodeJS has file watching API (https://nodejs.org/docs/latest/api/fs.html#fs_class_fs_fswatcher), thus, you can subscribe to FS events.
  2. You should notify client about this changes. I would use soket.io ( https://socket.io/ , both client and server side).

Using the file watcher and websokets you can notify user about any FS changes. You can upload files using FTP, HTTP client or just create them locally.

Alex
  • 4,621
  • 1
  • 20
  • 30
1

Clientside/Frontend Languages won't able to create/edit/delete a File

It can only read a File

for writefile in node js ..its already in stackoverflow refer Writing files in Node.js

Janen R
  • 729
  • 10
  • 21