업로드시 원본 파일을 얻으면 날짜가 생성됩니다
-
14-09-2019 - |
문제
파일을 웹 사이트에 업로드하는 프로세스가 있습니다. 해당 파일이 언제 생성되었는지 확인할 수있는 것이 중요해졌습니다. httppostedfile에서 원래 생성 날짜를 추출 할 수있는 방법을 찾고 있습니다. 누군가 나에게 아이디어가 있다면 정말 감사합니다 (이 시점에서 약간 그루터기가 있습니다).
해결책 2
여기 내가 끝난 해결책이 있습니다. 파일을 업로드하고 서버에 저장 한 후에는 파일의 메타 데이터에 액세스 할 수 있습니다 (이 솔루션은 현재 이미지 파일에만 적용됩니다. 전체 메타 데이터를 표시하는 데 사용할 수있는 추가 코드도 있습니다. 필요한 경우 파일과 내가 해킹 한 메타 데이터에서 더 깨끗하게 할 수있는 이상한 날짜를 발견했습니다.) ...
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();
}
다른 팁
클라이언트에서 파일이 생성 된 날짜에 액세스 할 수 없습니다. 피들러를 사용하여이를 검증 할 수 있습니다. 게시 할 수있는 유일한 데이터는 파일 이름과 MIME 유형이라고 생각합니다.
위의 Bryon이 언급 한 접근법을 시도했지만 날짜가 잘못되었습니다. 즉, 1600 년경에 무언가.
그러나 FileUpload Control의 파일 속성을 통해 'LastModifiedDate'속성에서 업로드 된 파일의 날짜를 얻을 수 있습니다.
다음은 샘플 html/javaScript입니다. 나는 그것을 가져왔다 :
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>
예를 들어 jQuery 파일 업로드 컨트롤을 사용 하여이 정보를 추가 매개 변수로 전달할 수 있습니다. 다음은 이것을 보여주는 링크입니다.
httppostedfile :: filename에서 파일 시스템 생성 날짜를 가져옵니다.
다음과 같은 것 :
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);