开发者

check uploaded file in vb.net

开发者 https://www.devze.com 2023-03-16 19:47 出处:网络
I need a snippet to check fi开发者_开发问答le for validity (I\'m allowing users to upload xml files). So I need to check whether uploaded file is XML.

I need a snippet to check fi开发者_开发问答le for validity (I'm allowing users to upload xml files). So I need to check whether uploaded file is XML. The best I can think of is just check if extension is ".xml". What if its replaced?


You can try loading it like this and catch the exception:

XDocument xdoc = XDocument.Load("data.xml"));


Presumably, if they're uploading XML, then you're going to use it for something afterwards. In this case you should validate the XML against a Schema (XSD etc) so that you know you aren't going to hit unexpected values/layouts etc.


In Urlmon.dll, there's a function called FindMimeFromData.

From the documentation

MIME type detection, or "data sniffing," refers to the process of determining an appropriate MIME type from binary data. The final result depends on a combination of server-supplied MIME type headers, file extension, and/or the data itself. Usually, only the first 256 bytes of data are significant.

So, read the first (up to) 256 bytes from the file and pass it to FindMimeFromData.


If you must validate the xml (assuming you want to validate the entire thing) you can use the XmlDocument class and catch an exception if it's not XML.

0

精彩评论

暂无评论...
验证码 换一张
取 消