13

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?

0

5 Answers 5

12

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

Sign up to request clarification or add additional context in comments.

1 Comment

I had to use files[i].lastModified instead of files[i].lastModifiedDate to make it work.
8

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

4 Comments

Yes, HTTP post request does not contaion this information, but the file itelself must be having this information(May be in file Headers). Can you tell me, is there any way to read these file headers in c#, or correct me, if my logic is wrong?
@Rau, your question mentions txt files. Arbitrary plain text files won't have such headers. html files probably also won't, unless that information is added by e.g. a source control system. What you want to achieve doesn't seem to be possible in the general case.
@Frédéric Hamidi: Thanks Frederic,I think txt and html files does contain these headers, bcoz i am using Juploader for uploading files, it does show last modified date of txt and html file. I think to get this information i need to read those headers from file, which seems logical to me, but i dont know how to do it C#.
@Rua: I can assure you that these files do not have that information in the headers. Text files don't even have a header. In case you meant jUploader, that's a Java solution so it isn't bound to the limitations of the HTTP protocol.
6

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.

5 Comments

Will this date automatically be the current date ( or the date it has been uploaded) ?
Uw, see amended text above ;)
Ya jim, i know about this, but i want to retrive last modified date of file before saving it on the server. According to my knowledge each file contains this information in to file headers, so is there any way to read these file headers in C#, correct me if my logic is wrong?
Rau - I've never come across this in my experience. that's not to say there's not a way. i'll dig a bit deeper and update if i find anything. but the http protocol doesn't specify this behaviour, so it may be a client side flash solution (i.e. as part of the upload control) that may be the best compromise
Thanks for your reply, and the interest that your showing. As I have said, there can not be a straight answer to this. Off course one need to go in raw level of reading file headers. And let me inform if you get any details. Thanks one more time.
3

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

4 Comments

And then its last modified time will be the current time.
Exactly. The problem you expose is quite complicated. I'm discussing with several colleagues and we do not believe you can do. : (
Maybe... with a little javascript? The javascript read from de file before upload it.
Thanks for your reply, but as per my knowledge javascript can not access to the File System of client machine(It is big security problem). If you know anything more on this, please let me know ?
0

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.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.