Search code examples
.netfile-uploadhttppostedfiledatecreated

Getting the original files create date upon upload


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


Solution

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