ファイルをウェブサイトにアップロードするプロセスが用意されています。これらのファイルがいつ作成されたかを確認できることが、ユーザーにとって重要になっています。HttpPostedFile から元の作成日を抽出する方法を探しています。誰かが私にアイデアを持っているなら、私はそれを本当に感謝しています (私はこの時点で少し困惑しています)。
4 に答える
クライアントでファイルが作成された日付にアクセスすることはできません。Fiddler を使用してこれを検証できます。投稿されているデータは、ファイル名と MIME タイプだけだと思います。
上記の Bryon によって言及されたアプローチを試しましたが、間違った日付が表示されます。つまり、1600 年頃のことです。
ただし、FileUpload コントロールの files プロパティを介して、「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 ファイル アップロード コントロールを使用して、この情報を追加のパラメーターとして渡すことができます。これを示すリンクは次のとおりです。
これが私が最終的に得た解決策です。ファイルをアップロードしてサーバーに保存すると、ファイル内のメタデータにアクセスできます (ただし、このソリューションは現在、画像ファイルにのみ適用されます。メタデータ全体を表示するために使用できる追加のコードもあります。必要に応じてファイルを削除し、ハッキングしたメタデータに奇妙な日付形式を見つけたので、おそらくもっときれいにすることができます)...
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();
}
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);