plor
plor

Reputation: 282

Access local file modification time in JavaScript

Is there a way to get the modification time of a file (either ctime or mtime should work) that is accessed locally through JavaScript?

I want to go to file:///home/me/mtime.html and have the JavaScript tell me that /home/me/file.txt was modified 2 minutes ago or something. I Understand that JavaScript has limited file access due to security problems, but is there some trick since it is all done locally.

Thanks.

Upvotes: 5

Views: 2916

Answers (4)

Denis Sadowski
Denis Sadowski

Reputation: 3133

Firefox has a set of components under its XPCOM (ActiveX competitor technology) that could be used to do the same thing.

Probably something like this (untested):

function getLastModifiedTime(filePath) 
{
    try 
    {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    } 
    catch (e) 
    {
        throw new Error("Permission to read file was denied.");
    }

    var file = Components.classes["@mozilla.org/file/local;1"]
    .createInstance(Components.interfaces.nsILocalFile);

    file.initWithPath( filePath );

    return file.lastModifiedTime;
}

As for Safari.... no idea. Maybe a signed java applet?

Upvotes: 1

Denis Sadowski
Denis Sadowski

Reputation: 3133

Here is some javascript using ActiveX that I think might help you out:

<script language=jscript runat=server> 
    var thisfile = <File_Path>; 
    thisfile = Server.MapPath(thisfile); 
    var fso = new ActiveXObject("Scripting.FileSystemObject"); 
    var fs = fso.GetFile(thisfile); 
    var dlm = fs.DateLastModified; 
    Response.Write("Last modified: " + dlm); 
</script>

If you need how long ago it was modified you would need some other javascript to subtract dlm from the current time.

Upvotes: 1

nekman
nekman

Reputation: 1919

Sorry but it's not possible with JavaScript.

Upvotes: 0

Peter Bailey
Peter Bailey

Reputation: 105878

Probably through ActiveX or some other browser component that lets the user grant extended permissions to the browser, such as an HTA or through something like Google Gears.

In otherwords, "No", unless you're willing to do something non-standard.

Upvotes: 0

Related Questions