javascript 如何获取上传文件的最后修改日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8285733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:56:02  来源:igfitidea点击:

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

javascriptjqueryasp.netxmlfile-upload

提问by Anyname Donotcare

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.

我上传了一个 XML 文件以将其内容迁移到我的数据库,但我想首先存储该文件的最后修改日期以确保该文件没有发生任何更改。

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

如何获取文件的最后修改日期?

Is there any javascript function to do that?

是否有任何 javascript 函数可以做到这一点?

回答by Darin Dimitrov

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 APIto obtain this information from the file before uploading it.

当您使用文件输入上传文件时,此信息永远不会发送到服务器。仅文件名、mime 类型和内容以multipart/form-data. 在上传文件之前,您可以使用HTML5 文件 API从文件中获取此信息。



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:

根据评论部分的要求,这里有一个 ASP.NET 页面的示例,它有一个上传控件和一个隐藏字段,用于使用 HTML5 文件 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 onchangeevent 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.

所以在这个例子中,我们订阅onchange了文件输入的事件,如果客户端浏览器支持 HTML5 File API,我们可以获得有关所选文件的信息,例如其名称、大小、上次修改日期……在这个例子中,我们存储最后修改日期到一个隐藏字段,这样一旦我们上传文件,这些信息就可以在服务器上使用。

回答by LesterDove

System.IO.FileInfo object should yield a LastWriteTime property

System.IO.FileInfo 对象应该产生一个 LastWriteTime 属性

FileInfo myFileInfo= new FileInfo(path) ;
myFileInfo.Refresh();
string t = myFileInfo.LastWriteTime.ToString("F")