C#检测上传图片是否安全函数

//添加引用System.Text;

//添加引用System.IO;

/// <summary>
/// C#检测上传图片是否安全函数
/// </summary>
/// <param name="strPictureFilePath"></param>
public void CheckPictureSafe(string strPictureFilePath)
{
bool strReturn = true;
if (!File.Exists(strPictureFilePath))
{
StringBuilder str_Temp = new StringBuilder();
try
{
using (StreamReader sr = new StreamReader(strPictureFilePath)) //按文本文件方式读取图片内容
{
String line;
while ((line = sr.ReadLine()) != null)
{
str_Temp.Append(line + ",");
}
//检测是否包含危险字符串
if (str_Temp == null)
{
strReturn = false;
}
else
{
str_Temp = str_Temp.Replace("'", "''");
string DangerString = "script|iframe|.getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory|.saveas|wscript.shell|script.encode|server.|.createobject|execute|activexobject|language=|include|filesystemobject|shell.application";
string[] sArray = DangerString.Split('|');
foreach (string i in sArray)
{
strReturn = true;
break;
}
}
sr.Close();
}
if (strReturn)
{
File.Delete(strPictureFilePath);
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值