質問

ファイルをWebサイトにアップロードするプロセスがあります。ユーザーにとって、これらのファイルがいつ作成されたかを確認できることが重要になりました。 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」プロパティから(be)アップロードされたファイルの日付を取得できます。

これが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ファイルアップロード制御を使用して追加のパラメーターとして渡すことができます。これを示しているリンクは次のとおりです。

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);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top