如何获得上传文件的最后修改日期? [英] How to get the last modified date of the uploaded file?

查看:132
本文介绍了如何获得上传文件的最后修改日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问:

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

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.

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

是否有任何的JavaScript 函数来做到这一点?

Is there any javascript function to do that?

推荐答案

当你使用一个文件输入上传文件时,此信息不会发送到服务器。只有文件名,MIME类型和内容与的multipart / form-data的发送。您可以使用 HTML5文件API 上传之前获得的文件信息。

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.

由于在评论部分要求这里是它有一个上传控件和一个隐藏字段将被用于存储和发送文件最后修改日期为使用HTML5文件API服务器的ASP.NET页面的一个例子:

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>

因此​​,在这个例子中,我们认购文件输入的的onchange 事件,如果客户端浏览器支持HTML5文件API,我们可以得到关于所选文件的信息,例如其名称,大小,最后修改日期,...在这个例子中,我们的最后修改日期存储到一个隐藏字段,以便这些信息可在服务器上,一旦我们上传的文件。

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.

这篇关于如何获得上传文件的最后修改日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆