Jump to content

Getting file info with javascript


Jani

Recommended Posts

Hi all,I am trying to do a simple javascript that traverses an array of filenames (local) and looks up their last modified date. I figured I could do this by loading each file to a hidden frame, waiting until the page has loaded (using setTimeout, couldn't get addeventlistener for 'load' to work on the hidden frame) and then getting document.lastModified. This works fine for most files, but files that launch an external application cause the script to stop.So I am wondering the following:1. Is there a way to only get the head of a file (containing lastModified) but not the contents?2. Is there a way to suppress the launching of associated applications for filetypes (.doc, .xls etc)?3. Is there any other way to get last modified date for a bunch of files with javascript?Would appreciate any help, thanks!

Link to comment
Share on other sites

This may or may not work but you could use AJAX to call the files ( Make sure to store the requests in an array so they have a dynamic name because two requests may not be launched with the same name at once ).When the file is called just use the getResponseHeader("Last-Modified") method of the XMLHttpRequest object and it should give you the required data.For number two, AJAX will most likely not launch the files as it returns the data in a text string.:)

Link to comment
Share on other sites

Yeah, with AJAX you can even send a HEAD request rather than a GET or POST - it might speed things up a bit. You'll have to get the file information one file at a time.

Link to comment
Share on other sites

Excellent, thank you guys!I even found an example to do this using Ajax: xmlhttp.open("HEAD", "/faq/index.html",true); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4) { alert("File was last modified on - "+ xmlhttp.getResponseHeader("Last-Modified")) } } xmlhttp.send(null)Seems like it's time to start learning Ajax now...

Link to comment
Share on other sites

*sigh*This looks like it should be working, but I don't get any headers!If I do xmlhttp.getAllResponseHeaders() I get null as the result.I get the content of the files (even when doing a "HEAD" request), but I don't get the headers... anyone know why???I made a simple test out of some code I googled:function newRequest(){ var xmlhttp=false; /*@cc_on @*/ /*@if (@_jscript_version >= 5) // JScript gives us Conditional compilation, we can cope with old IE versions. // and security blocked creation of the objects. try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } } @end @*/ if (!xmlhttp && typeof XMLHttpRequest!='undefined') { try { xmlhttp = new XMLHttpRequest(); } catch (e) { xmlhttp=false; } } if (!xmlhttp && window.createRequest) { try { xmlhttp = window.createRequest(); } catch (e) { xmlhttp=false; } } return xmlhttp;}function test(){ xmlhttp = newRequest(); xmlhttp.open("HEAD", "doc.txt",true); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4) { alert('statusText = '+xmlhttp.statusText+'\nresponseText = '+xmlhttp.responseText+'\nAllResponseHeaders = '+xmlhttp.getAllResponseHeaders()) } } xmlhttp.send(null)}The alert pops up with a blank statusText, responseText containing the content of doc.txt and AllResponseHeaders = null

Link to comment
Share on other sites

Are you running this through a web server or are you running it from your file system? I mean, in the address bar of your browser, does it read something like "http://localhost/myfile.html" or is it something like "c:\files\myfile.html".I believe, in order to get this to work, you'll have to run it through a web server.Yeah, just ran a test locally. When I ran "file:///C:/Inetpub/wwwroot/fileinfotest.html", I got a null response for the headers but when I ran "http://localhost/fileinfotest.html", the headers returned as normal and I could get the Last-Modified date from the file. Which, when I think more about it, makes sense since we are trying to get the headers that come from the response from the web server. If you aren't being served this file from a web server, there aren't going to be any response headers.Hope this helps!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...