获取原始文件创建日期后上传 [英] Getting the original files create date upon upload

查看:205
本文介绍了获取原始文件创建日期后上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经制定一个过程,上传文件到我们的网站。它已成为重要的用户能够查看创建这些文件时。我正在寻找一种方法来提取HttpPostedFile原来的创建日期。如果任何人有一个想法,对我来说我真的AP preciate它(我有点难倒在这一点)。

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

推荐答案

这里的解决方案,我结束了。一旦你上传的​​文件并将其保存到服务器上,您可以访问元数据文件中(该解决方案然而,目前仅适用于图像文件 - 这里还有一些额外的code。在那里,可以用来显示如果需要对整个文件的元数据,我发现了一些奇怪的日期格式化的,我砍死周围很可能做清洁的元数据)......

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

这篇关于获取原始文件创建日期后上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆