views:

586

answers:

3

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

A: 

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);
Byron Whitlock
This only works if the file you upload came from the web server itself.
David
I see, I misread as the file creation date on the server, not the client.
Byron Whitlock
Yeah I had originally headed down that path but what you end up with is 12/31/1600.
+1  A: 

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.

David
A: 

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