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).
我们有一个将文件上传到我们网站的流程。用户能够看到创建这些文件的时间变得很重要。我正在寻找一种从HttpPostedFile中提取原始创建日期的方法。如果有人对我有所了解,我会非常感激(我在这一点上有点难过)。
4 个解决方案
#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.
您无权访问在客户端上创建文件的日期。您可以使用Fiddler来验证这一点。我相信你会看到的唯一数据是文件名和mime类型。
#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.
我尝试过Bryon上面提到的方法,但它给了我错误的日期。即1600年左右的事情。
You can however get the Date for each (to be) uploaded file from the 'lastModifiedDate' property via FileUpload control's files property.
但是,您可以通过FileUpload控件的files属性从'lastModifiedDate'属性获取每个(将要)上载文件的日期。
Here is the sample HTML/Javascript for it. I have taken it from:
这是它的示例HTML / Javascript。我把它从:
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.
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_fileupload_files并根据我们的需要对其进行了一些修改。注意:请在此HTML / Javascript代码段后阅读下面的评论。
<!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文件上载控件将此信息作为附加参数传递。以下是展示此内容的链接:
jquery file upload module sending extra parameter
jquery文件上传模块发送额外参数
#4
You just grab the file system creation date from the HttpPostedFile::FileName.
您只需从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);
#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.
您无权访问在客户端上创建文件的日期。您可以使用Fiddler来验证这一点。我相信你会看到的唯一数据是文件名和mime类型。
#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.
我尝试过Bryon上面提到的方法,但它给了我错误的日期。即1600年左右的事情。
You can however get the Date for each (to be) uploaded file from the 'lastModifiedDate' property via FileUpload control's files property.
但是,您可以通过FileUpload控件的files属性从'lastModifiedDate'属性获取每个(将要)上载文件的日期。
Here is the sample HTML/Javascript for it. I have taken it from:
这是它的示例HTML / Javascript。我把它从:
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.
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_fileupload_files并根据我们的需要对其进行了一些修改。注意:请在此HTML / Javascript代码段后阅读下面的评论。
<!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文件上载控件将此信息作为附加参数传递。以下是展示此内容的链接:
jquery file upload module sending extra parameter
jquery文件上传模块发送额外参数
#4
You just grab the file system creation date from the HttpPostedFile::FileName.
您只需从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);