Getting the original files create date upon upload

六月ゝ 毕业季﹏ 提交于 2019-12-08 07:56:52

问题


We have a process in place that uploads files to our website. It has become important to the users to be able to see when those files were created. I'm looking for a way to extract the original create date from the HttpPostedFile. If anyone has an idea for me I'd really appreciate it (I'm a bit stumped at this point).


回答1:


You don't have access to the date the file was created on the client. You can use Fiddler to validate this. I believe the only data you'll see being posted is the filename and the mime type.




回答2:


Here's the solution I ended up with. Once you've uploaded the file and saved it to the server you can access the metadata in the file (this solution however, only currently applies to image files - there's also some extra code in there that could be used to show the entire metadata for the file if needed, and I found some weird date formating in the metadata that I hacked around that could probably be done cleaner)...

                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();
                  }



回答3:


I tried the approach mentioned by Bryon above but it gives me incorrect date. i.e something around year 1600.

You can however get the Date for each (to be) uploaded file from the 'lastModifiedDate' property via FileUpload control's files property.

Here is the sample HTML/Javascript for it. I have taken it from:

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_fileupload_files and modified it a bit for our need. Note: Please read my comment below after this HTML /Javascript snippet.

<!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>

You can pass this information as an additional parameter using jQuery file upload control for example. Here is the link demonstrating this:

jquery file upload module sending extra parameter




回答4:


You just grab the file system creation date from the HttpPostedFile::FileName.

Somthing like this:

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);


来源:https://stackoverflow.com/questions/998098/getting-the-original-files-create-date-upon-upload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!