How to get a file creation time with file upload browser
I have tried below code but getting 01/01/1601 05:30 AM
DateTime originalCreationDate = File.GetCreationTime(fileUpdBankStatement.PostedFile.FileName);
DateTime originalModificationDate = File.GetLastWriteTime(fileUpdBankStatement.FileName);
// Set the original metadata on the uploaded file
File.SetCreationTime(dirPath + fileName, originalCreationDate);
File.SetLastWriteTime(dirPath + fileName, originalModificationDate);
Aman GuptaPosted Sep 7, 2024, 6:04 PM
Hi Vikram,
The issue you are encourting is because when you upload a file using a browser, the file metadata such as creation time and last modification time are not preserved. Browsers do not pass the file's original metadata (like creation or modification time) when the file is uploaded due to security and privacy reasons. As a result, methods like File.GetCreationTime() and File.GetLastWriteTime() will return the system default value (such as 01/01/1601 05:30 AM on Windows systems) for the uploaded file.
Why this happens?
As I already told above why this happens, I'm summerizing it here for you.
Proposed Solutions
Example:
In this case, the file creation and modification times will match the upload time.
This will be help you Vikram, hope this helps you.
Thanks
Jayraj ChhayaPosted Sep 6, 2024, 1:05 PM
The issue you are facing is likely due to the way the file path is being handled. When you use
File.GetCreationTime(fileUpdBankStatement.PostedFile.FileName), it may not be pointing to the correct file path on the server, especially if the file has not been saved to the server yet.To correctly retrieve the creation time of an uploaded file, you should first save the file to a temporary location on the server and then access its properties. Here’s an example of how to do this:
By saving the file first, you ensure that you are working with the correct file path, which should resolve the issue of getting an incorrect creation time.