How to get the last modified date of the uploaded file?

后端 未结 2 947
鱼传尺愫
鱼传尺愫 2021-01-22 07:34

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

相关标签:
2条回答
  • 2021-01-22 08:01

    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.

    0 讨论(0)
  • 2021-01-22 08:20

    System.IO.FileInfo object should yield a LastWriteTime property

    FileInfo myFileInfo= new FileInfo(path) ;
    myFileInfo.Refresh();
    string t = myFileInfo.LastWriteTime.ToString("F") 
    
    0 讨论(0)
提交回复
热议问题