Is there a simple way to get the FileInfo
object from an HttpPostedFileBase
? I realize I can save the file then do something like DirectoryInfo.GetFiles
and then loop through the files looking for my file, but is there any simpler way to do this for a given file?
There's no FileInfo
associated to an uploaded file. Only the filename
is sent as parameter as well as the file stream itself. So that's what you could query:
HttpPostedFileBase file = ...
string filename = file.FileName;
int fileSize = file.ContentLength;
string contentType = file.ContentType;
using (Stream stream = file.InputStream)
{
// do something with the file contents here
}
To better understand what gets sent from the client I invite you to read the multipart/form-data
specification.
The FileInfo object contains things like LastModified and LastAccessed date which is not an information that is sent when a file is uploaded. If you save the file on your web server disk and then retrieve the FileInfo from it bear in mind that what you will be retrieving is the information about this file on the server and not on the client simply because this information is never sent when a file is uploaded.
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