5 .net file-upload httppostedfile datecreated
我们有一个将文件上传到我们网站的流程。对于用户来说,能够看到这些文件的创建时间变得很重要。我正在寻找一种从 HttpPostedFile 中提取原始创建日期的方法。如果有人对我有想法,我会非常感激(此时我有点困惑)。
小智 1
这是我最终得到的解决方案。上传文件并将其保存到服务器后,您可以访问文件中的元数据(但是,此解决方案当前仅适用于图像文件 - 那里还有一些额外的代码可用于显示整个元数据如果需要的话,该文件,并且我在我破解的元数据中发现了一些奇怪的日期格式,这可能可以做得更干净)...
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();
}
Run Code Online (Sandbox Code Playgroud)