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?
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
You can't do this. An HTTP post request does not contain this information about an uploaded file.
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.
It is not possible, until you save the file to disk.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With