Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve "Last Modified Date" of uploaded file in ASP.Net

Tags:

c#

asp.net

I am developing a website, in which client uploads some document files like doc, docx, htm, html, txt, pdf etc. I want to retrieve last modified date of an uploaded file. I have created one handler(.ashx) which does the job of saving the files.

Following is the code:
HttpPostedFile file = context.Request.Files[i];                                 
string fileName = file.FileName;                               
file.SaveAs(Path.Combine(uploadPath, filename));

As you can see, its very simple to save the file using file.SaveAs() method. But this HttpPostedFile class is not exposing any property to retrieve last modified date of file.

So can anyone tell me how to retrieve last modified date of file before saving it to hard disk?

like image 639
Rau Avatar asked Mar 03 '11 12:03

Rau


4 Answers

Today you can access to this information from client side using HTML5 api

//fileInput is a HTMLInputElement: <input type="file" multiple id="myfileinput"> 
var fileInput = document.getElementById("myfileinput");
// files is a FileList object (simliar to NodeList) 
var files = fileInput.files;
for (var i = 0; i < files.length; i++) {
    alert(files[i].name + " has a last modified date of " + files[i].lastModifiedDate);
}

Source and more information

like image 168
gsubiran Avatar answered Nov 19 '22 10:11

gsubiran


You can't do this. An HTTP post request does not contain this information about an uploaded file.

like image 8
Elian Ebbing Avatar answered Nov 19 '22 12:11

Elian Ebbing


Rau,

You can only get the date once it's on the server. If you're ok with this, then try:

string strLastModified = 
    System.IO.File.GetLastWriteTime(Server.MapPath("myFile.txt")).ToString("D");

the further caveat here being that this datetime will be the date at which it was saved on the server and not the datetime of the original file.

like image 6
jim tollan Avatar answered Nov 19 '22 10:11

jim tollan


It is not possible, until you save the file to disk.

like image 3
Taber Avatar answered Nov 19 '22 11:11

Taber