How to get file extension from the request.files开发者_JAVA技巧 collection in asp.net?
I think this should do the trick:
foreach (HttpPostedFile file in Request.Files) {
string extension = System.IO.Path.GetExtension(file.FileName);
}
Each HttpPostedFile
in Request.Files
has a FileName
that includes the extension; to get just the extension, use Path.GetExtension(file.FileName)
Note that using the content-type may be more reliable in many cases.
Request.Files is actually a Name Object Collection so this code would work better
foreach (string keyName in Request.Files) {
string extension = System.IO.Path.GetExtension(Request.Files[keyName].FileName);
}
and this code worked for me.
精彩评论