Ottenere i file originali creare data al momento del caricamento
-
14-09-2019 - |
Domanda
Abbiamo in atto un processo che carica i file al nostro sito web. E 'diventato importante per gli utenti di essere in grado di vedere quando sono stati creati i file. Sto cercando un modo per estrarre la data di creazione originale dal HttpPostedFile. Se qualcuno ha un'idea per me mi piacerebbe davvero apprezzare (io sono un po 'perplesso a questo punto).
Soluzione 2
Ecco la soluzione ho finito con. Una volta caricato il file e salvato sul server è possibile accedere i metadati nel file (questa soluzione tuttavia, solo attualmente si applica ai file di immagine - c'è anche un po 'di codice aggiuntivo in là che potrebbero essere utilizzati per mostrare l'intero metadati per il file, se necessario, e ho trovato un po 'strano data di formattazione nei metadati che ho inciso in giro che potrebbe essere fatto probabilmente più pulita) ...
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();
}
Altri suggerimenti
Non è necessario l'accesso alla data di creazione del file sul client. È possibile utilizzare Fiddler per convalidare questo. Credo che gli unici dati vedrete di essere pubblicati è il nome del file e il tipo MIME.
ho provato l'approccio citato da Bryon sopra, ma mi dà data errata. cioè qualcosa intorno all'anno 1600.
È possibile comunque ottenere la data per ogni (essere) caricata dalla proprietà 'LastModifiedDate' via proprietà file del controllo FileUpload.
Ecco il codice HTML di esempio / Javascript per esso. Ho preso da:
http://www.w3schools.com/jsref/tryit.asp ? nomefile = tryjsref_fileupload_files e modificato un po 'per il nostro bisogno. Nota:. Si prega di leggere il mio commento qui sotto dopo questo HTML / Javascript frammento
<!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>
È possibile passare questa informazione come un parametro aggiuntivo utilizzando jQuery controllo di caricamento di file, ad esempio. Ecco il link che dimostrano questo:
Basta afferrare la data di creazione del file system dal HttpPostedFile :: nome del file.
Somthing in questo modo:
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);