验证文件扩展名 [英] Validate file extension

查看:141
本文介绍了验证文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须上传扩展名为.fnm的文件

以下是我的验证方式:



I have to upload the file which has the extension .fnm
Here is how I am validating:

If String.Compare(System.IO.Path.GetExtension(fuUpload.FileName), ".fnm", True) <> 0





工作正常。它只上传扩展名为.fnm的文件。



问题是当有人操作文件扩展名时,例如将.txt或.doc更改为.fnm ,它绕过验证并上传。







有什么方法可以验证其扩展名以外的文件类型?或任何其他验证方法。请建议。



And its working fine. It only uploads the file having the extension .fnm.

The problem is that when someone manipulates the file extension like changes .txt or .doc to .fnm, it bypasses the validation and gets uploaded.



Is there any way to validate file type other than its extension? Or any other method to validate. Please suggest.

推荐答案

有几种方法,首先你可以读取mime类型,但只能识别众所周知的文件类型。在这种情况下,我认为最好的方法是在处理之前打开并读取文件并验证文件的内容。这只是为了确定。
There are a couple of ways, first you can read the mime type, but only well known file types will be recognized. In this case, I think the best way would be to open and read the file and validate to file's content before handling it. That's to only way to be sure.


http://stackoverflow.com/questions/300406/validating-an-uploaded-files-contenttype-in​​-asp-net [ ^ ]



试试这个链接
http://stackoverflow.com/questions/300406/validating-an-uploaded-files-contenttype-in-asp-net[^]

try this link


请检查以下链接



Asp.net文件上传控件



文件上传HTML控件


这篇关于验证文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆