Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 上传时获取原始文件创建日期_.net_File Upload_Httppostedfile_Datecreated - Fatal编程技术网

.net 上传时获取原始文件创建日期

.net 上传时获取原始文件创建日期,.net,file-upload,httppostedfile,datecreated,.net,File Upload,Httppostedfile,Datecreated,我们有一个将文件上传到我们网站的流程。对于用户来说,能够看到这些文件是何时创建的变得非常重要。我正在寻找一种从HttpPostedFile中提取原始创建日期的方法。如果有人对我有什么想法,我会非常感激(我现在有点不知所措)。您只需从HttpPostedFile::FileName获取文件系统创建日期即可 像这样的事情: HttpFileCollection MyFileColl = Request.Files; HttpPostedFile MyPostedFile = MyFileColl.G

我们有一个将文件上传到我们网站的流程。对于用户来说,能够看到这些文件是何时创建的变得非常重要。我正在寻找一种从HttpPostedFile中提取原始创建日期的方法。如果有人对我有什么想法,我会非常感激(我现在有点不知所措)。

您只需从HttpPostedFile::FileName获取文件系统创建日期即可

像这样的事情:

HttpFileCollection MyFileColl = Request.Files;
HttpPostedFile MyPostedFile = MyFileColl.Get(0);
String filename = MyPostedFile.FileName;
String creationTime;

if (File.Exists(fileName)) 
{
      creationTime = File.GetCreationTime(fileName).ToString(); 
}
System.writeLine(creationTime);

您无权访问在客户端上创建文件的日期。您可以使用Fiddler来验证这一点。我相信您将看到发布的唯一数据是文件名和mime类型。

这是我最终得到的解决方案。上载文件并将其保存到服务器后,可以访问文件中的元数据(然而,这个解决方案目前只适用于图像文件——如果需要,还可以使用一些额外的代码来显示文件的整个元数据,我在元数据中发现了一些奇怪的日期格式,我对其进行了黑客攻击,这可能会更干净)


我尝试了上面Bryon提到的方法,但它给出了错误的日期,即大约1600年

但是,您可以通过FileUpload控件的files属性从“lastModifiedDate”属性获取每个(待)上载文件的日期

下面是它的示例HTML/Javascript。 我是从以下地方取的:

并根据我们的需要对其进行了一些修改。注意:请在这段HTML/Javascript代码片段之后阅读我下面的评论

<!DOCTYPE html>
<html>
<body onload="myFunction()">

<input type="file" id="myFile" multiple size="50" onchange="myFunction()">

<p id="demo"></p>

<script>
function myFunction(){
    var x = document.getElementById("myFile");
    var txt = "";
    if ('files' in myFile) {
        if (x.files.length == 0) {
            txt = "Select one or more files.";
        } else {
            for (var i = 0; i < x.files.length; i++) {
                txt += "<br><strong>" + (i+1) + ". file</strong><br>";
                var file = x.files[i];
                if ('name' in file) {
                    txt += "name: " + file.name + "<br>";
                }
                if ('size' in file) {
                    txt += "size: " + file.size + " bytes <br>";
                }
                if ('lastModifiedDate' in file) {
                    txt += "lastModifiedDate: " + file.lastModifiedDate.toString();
                }
            }
        }
    } 
    else {
        if (x.value == "") {
            txt += "Select one or more files.";
        } else {
            txt += "The files property is not supported by your browser!";
            txt  += "<br>The path of the selected file: " + x.value; // If the browser does not support the files property, it will return the path of the selected file instead. 
        }
    }
    document.getElementById("demo").innerHTML = txt;
}
</script>

<p><strong>Tip:</strong> Use the Control or the Shift key to select multiple files.</p>

</body>
</html>

函数myFunction(){ var x=document.getElementById(“myFile”); var txt=“”; 如果(myFile中的“文件”){ 如果(x.files.length==0){ txt=“选择一个或多个文件。”; }否则{ 对于(var i=0;i”+(i+1)+“。文件
”; var file=x.files[i]; 如果(文件中的“名称”){ txt+=“名称:”+file.name+“
”; } 如果(文件中的“大小”){ txt+=“大小:”+file.size+“字节数
”; } if('lastModifiedDate'在文件中){ txt+=“lastModifiedDate:”+file.lastModifiedDate.toString(); } } } } 否则{ 如果(x.value==“”){ txt+=“选择一个或多个文件。”; }否则{ txt+=“您的浏览器不支持文件属性!”; txt+=“
所选文件的路径:”+x.value;//如果浏览器不支持“文件”属性,它将返回所选文件的路径。 } } document.getElementById(“demo”).innerHTML=txt; } 提示:使用控件或Shift键选择多个文件

例如,可以使用jQuery文件上载控件将此信息作为附加参数传递。 以下是演示此功能的链接:


这只适用于您上传的文件来自web服务器本身的情况。我知道,我误读了服务器上的文件创建日期,而不是客户端。是的,我最初是沿着这条路径走的,但最终的结果是1600年12月31日。如果您使用FTP/SCP将文件复制到网站,您可以设置一个选项来保留原始文件创建日期atesI必须使用file.lastModified now而不是file.lastModifiedDate.Yes,
。lastModified
是现在的做法,另一个已被弃用,如中所示。
<!DOCTYPE html>
<html>
<body onload="myFunction()">

<input type="file" id="myFile" multiple size="50" onchange="myFunction()">

<p id="demo"></p>

<script>
function myFunction(){
    var x = document.getElementById("myFile");
    var txt = "";
    if ('files' in myFile) {
        if (x.files.length == 0) {
            txt = "Select one or more files.";
        } else {
            for (var i = 0; i < x.files.length; i++) {
                txt += "<br><strong>" + (i+1) + ". file</strong><br>";
                var file = x.files[i];
                if ('name' in file) {
                    txt += "name: " + file.name + "<br>";
                }
                if ('size' in file) {
                    txt += "size: " + file.size + " bytes <br>";
                }
                if ('lastModifiedDate' in file) {
                    txt += "lastModifiedDate: " + file.lastModifiedDate.toString();
                }
            }
        }
    } 
    else {
        if (x.value == "") {
            txt += "Select one or more files.";
        } else {
            txt += "The files property is not supported by your browser!";
            txt  += "<br>The path of the selected file: " + x.value; // If the browser does not support the files property, it will return the path of the selected file instead. 
        }
    }
    document.getElementById("demo").innerHTML = txt;
}
</script>

<p><strong>Tip:</strong> Use the Control or the Shift key to select multiple files.</p>

</body>
</html>