2

I upload an XML file to migrate its contents to my database, but I want firstly store the last modified date of that file to assure that no change has happened to that file from the last one.

How do I get the last modified date of the file ?

Is there any javascript function to do that?

avs099
  • 10,937
  • 6
  • 60
  • 110
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392

2 Answers2

4

This information is never sent to the server when you use a file input to upload a file. Only the filename, mime type and contents are sent with multipart/form-data. You could use the HTML5 File API to obtain this information from the file before uploading it.


As requested in the comments section here's an example of an ASP.NET page which has an upload control and a hidden field which will be used to store and send the file last modified date to the server using the HTML5 File API:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Globalization" %>

<script type="text/C#" runat="server">
    protected void BtnUploadClick(object sender, EventArgs e)
    {
        var file = Request.Files[0];
        DateTime date;
        if (DateTime.TryParseExact(lastModifiedDate.Value, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
        {
            // you could use the date here
        }
    }
</script>

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form id="Form1" runat="server">
        <label for="up">Pick a file</label>
        <asp:FileUpload ID="up" runat="server" />
        <asp:HiddenField ID="lastModifiedDate" runat="server" />
        <asp:LinkButton ID="btnUpload" runat="server" Text="Upload" OnClick="BtnUploadClick" />
    </form>

    <script type="text/javascript">
        if (!window.File) {
            alert('Sorry, your browser doesn\'t support the File API so last modified date will not be available');
        } else {
            document.getElementById('<%= up.ClientID %>').onchange = function () {
                if (this.files.length > 0) {
                    if (typeof this.files[0].lastModifiedDate === 'undefined') {
                        alert('Sorry, your browser doesn\'t support the lastModifiedDate property so last modified date will not be available');
                    } else {
                        var lmDate = this.files[0].lastModifiedDate;
                        var hidden = document.getElementById('<%= lastModifiedDate.ClientID %>');
                        hidden.value = lmDate.getFullYear() + '-' + (lmDate.getMonth() + 1) + '-' + lmDate.getDate();
                    }
                }
            };
        }
    </script>
</body>
</html>

So in this example we subscribe for the onchange event of the file input and if the client browser supports HTML5 File API we can obtain information about the selected file such as its name, size, last modified date, ... In this example we store the last modified date into a hidden field so that this information is available on the server once we upload the file.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • hmmmm, then how to fix this problem? – Anyname Donotcare Nov 27 '11 at 13:15
  • 1
    @just_name, if you can support HTML5 then you could use the File API. If not, you basically cannot do it because you do not have access to this information on the client computer. You will have to add another input field where the user will be able to enter manually the last modified date. – Darin Dimitrov Nov 27 '11 at 13:17
  • 1
    @just_name, the code of LesterDove reads the information of a file that you already have stored on the server. If you have just uploaded this file from the client and stored it on the server it is far than obvious that this code will return you the current datetime because you have just created this file on the server. You will never get the original file modification date from the client for one simple reason that I already pointed out in my answer: using multipart/form-data this information is **never** sent to the server. – Darin Dimitrov Nov 27 '11 at 13:21
  • Excuse me, could u help me in using the `API` in my asp.net web application , i don't know what should i do to use it? – Anyname Donotcare Nov 27 '11 at 13:28
  • I will be grateful , if U could help me in getting this data. – Anyname Donotcare Nov 27 '11 at 13:35
  • 1
    @just_name, sure I can show you an example. Which technique do you want to see an example of? The File API? – Darin Dimitrov Nov 27 '11 at 13:55
  • I want to use this API in my web form asp.net application. – Anyname Donotcare Nov 27 '11 at 13:57
  • Could u give me the steps to get the last modified date through this `API`?because this is the first time to use an `API`! – Anyname Donotcare Nov 27 '11 at 13:58
  • Really thank u so much . the hidden field always is empty ! ` ` – Anyname Donotcare Nov 27 '11 at 14:11
  • 1
    @just_name, did you see my latest update in which I test if the browser supports the `lastModifiedDate` property? I have tested on IE9, FireFox 8 and Google Chrome and the only browser that supports this property currently is Google Chrome. – Darin Dimitrov Nov 27 '11 at 14:13
  • 1
    @just_name, which browser are you using? Also please test first my example in a new WebForm before implementing some third party controls. – Darin Dimitrov Nov 27 '11 at 14:14
  • oh god , this means that the only browser which support this property is the google chrome!!!!Is there any other method rather than this? – Anyname Donotcare Nov 27 '11 at 14:16
  • 1
    @just_name, yes, you could write and install some browser specific extensions: Silverlight, Flash, ActiveX, Java Applets, ... on the client browser that will allow you to access this information. With plain HTML and javascript the only official way is HTML5 File API which is currently in draft and not every browser supports it yet. So if you cannot do this you could use an additional input field where you would ask the user to enter the last modified date of the file he is about to upload. – Darin Dimitrov Nov 27 '11 at 14:17
0

System.IO.FileInfo object should yield a LastWriteTime property

FileInfo myFileInfo= new FileInfo(path) ;
myFileInfo.Refresh();
string t = myFileInfo.LastWriteTime.ToString("F") 
LesterDove
  • 3,014
  • 1
  • 23
  • 24