Rau
Rau

Reputation: 151

How to retrieve "Last Modified Date" of uploaded file in 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?

Upvotes: 13

Views: 16305

Answers (5)

gsubiran
gsubiran

Reputation: 2452

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].lastModified);
}

Source and more information

Upvotes: 12

James Lawruk
James Lawruk

Reputation: 31337

You typically cannot get the last modified date because the date is not stored in the file.

The Operating System actually stores file attributes like Created, Accessed, and Last Modified. See Where are “last modified date” and “last accessed date” saved?

(I say typically because certain file types like images may have EXIF tag data like the date/time the photo was taken.)

Upvotes: 0

jim tollan
jim tollan

Reputation: 22485

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.

Upvotes: 6

Taber
Taber

Reputation: 554

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

Upvotes: 3

Elian Ebbing
Elian Ebbing

Reputation: 19027

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

Upvotes: 8

Related Questions