Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get FileInfo for HttpPostedFileBase

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?

like image 704
Art F Avatar asked Jan 15 '23 10:01

Art F


1 Answers

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.

like image 118
Darin Dimitrov Avatar answered Jan 18 '23 22:01

Darin Dimitrov