上传时获取原始文件创建日期
-
14-09-2019 - |
题
我们有一个将文件上传到我们网站的过程。对于用户而言,能够查看这些文件何时创建了很重要。我正在寻找一种从HTTPPOSTEDFILE提取原始创建日期的方法。如果有人对我有一个主意,我将非常感谢它(此时我有点困难)。
解决方案 2
这是我最终得到的解决方案。将文件上传并将其保存到服务器后,您可以访问文件中的元数据(但是,此解决方案当前仅适用于图像文件 - 其中还有一些额外的代码可以用于显示整个元数据该文件如果需要,我发现在我围绕的元数据中发现了一些奇怪的日期,可能会更清洁)...
System.IO.FileInfo fileInfo = new System.IO.FileInfo(UPLOAD_DIRECTORY + file.FileName);
if (!fileInfo.Exists)
{
break;
}
else
{
//Check for metadata original create date
if (_imageFormats.Contains(fileInfo.Extension.ToLower()))
{
Stream fileStream = fileInfo.OpenRead();
System.Drawing.Image image = new System.Drawing.Bitmap(fileStream);
// Get the PropertyItems property from image.
System.Drawing.Imaging.PropertyItem[] propItems = image.PropertyItems;
// For each PropertyItem in the array, display the ID, type, and
// length.
int count = 0;
string s1 = null;
string dateID = null;
foreach (System.Drawing.Imaging.PropertyItem propItem in propItems)
{
s1 += "Property Item " + count.ToString() + "/n/r";
s1 += "iD: 0x" + propItem.Id.ToString("x") + "/n/r";
if (("0x" + propItem.Id.ToString("x")) == PROPERTYTAGEXIFDTORIG)
{
dateID = count.ToString();
}
s1 += "type: " + propItem.Type.ToString() + "/n/r";
s1 += "length: " + propItem.Len.ToString() + " bytes" + "/n/r";
count++;
}
// Convert the value of the second property to a string, and display
// it.
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
if (dateID != null)
{
string date = encoding.GetString(propItems[int.Parse(dateID)].Value);
date = date.Replace("\0", string.Empty);
string[] datesplit = date.Split(' ');
string newDate = datesplit[0].Replace(":", "-") + " " + datesplit[1];
originalCreateDate = DateTime.Parse(newDate);
}
fileStream.Close();
}
其他提示
您无权访问该文件在客户端上创建的日期。您可以使用Fiddler来验证此问题。我相信您会看到的唯一数据是文件名和MIME类型。
我尝试了上面Bryon提到的方法,但它给了我不正确的日期。即1600年左右的东西。
但是,您可以通过FileUpload Control的文件属性从“ LastModifiedDate”属性上载文件的每个(待定)文件的日期。
这是示例HTML/JavaScript。我从:
http://www.w3schools.com/jsref/tryit.asp?filename = tryjsref_fileupload_files并为我们的需求进行了一些修改。注意:此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>
您可以使用jQuery文件上传控件将此信息作为附加参数传递。这是证明这一点的链接:
您只需从httppostedfile ::文件名中获取文件系统创建日期。
这样的东西:
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);
不隶属于 StackOverflow