الحصول على الملفات الأصلية إنشاء تاريخ عند التحميل

StackOverflow https://stackoverflow.com/questions/998098

سؤال

لدينا عملية في مكانها تقوم بتحميل الملفات إلى موقعنا. أصبح من المهم للمستخدمين أن يكونوا قادرين على معرفة متى تم إنشاء تلك الملفات. أنا أبحث عن وسيلة لاستخراج تاريخ إنشاء الأصلي من 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();
                  }

نصائح أخرى

ليس لديك حق الوصول إلى تاريخ إنشاء الملف على العميل. يمكنك استخدام FIDLLER للتحقق من صحة هذا. أعتقد أن البيانات الوحيدة التي ستراه يجري نشرها هي اسم الملف ونوع MIME.

جربت النهج الذي ذكره براون أعلاه لكنه يعطيني موعد غير صحيح. أي شيء حوالي سنة 1600.

ومع ذلك، يمكنك الحصول على تاريخ الملف الذي تم تحميله لكل (ليكون) من خاصية "LastModifiedDate" الخاصة عبر خاصية ملفات التحكم Viripload.

هنا هو عينة 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 :: اسم الملف.

شيء مثل هذا:

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