不是用扩展名 真证检测文件 是否为有效文件 public static bool IsAllowedExtension(FileUpload hifile)
{
System.IO.FileStream fs = new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
string fileclass = "";
byte buffer;
try
{
buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString(); }
catch
{
}
r.Close();
fs.Close();
if (fileclass == "255216" || fileclass == "7173")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
{
return true;
}
else
{
return false;
} }
{
System.IO.FileStream fs = new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
string fileclass = "";
byte buffer;
try
{
buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString(); }
catch
{
}
r.Close();
fs.Close();
if (fileclass == "255216" || fileclass == "7173")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
{
return true;
}
else
{
return false;
} }
解决方案 »
- 这样的数据库表,应该怎么设计?
- 大家做MVC的时候都是这么建立的工程?
- 关于Request.QueryString转换
- 这两段代码有什么区别吗
- RegularExpressionValidator 中\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* 表达式什么意思.
- GridView 删除事件执行两次
- 急!!!动态加载TextBox控件的问题
- 想组织几个变量,写成标准的日期格式再插入数据库中,只有日期,不包括时间
- 放100分!!!焦急在线等待,从一个变量用C#生成一个静态页面,大家看这段代码怎么写!
- gridview中checkbox的问题
- 超简单,请帮帮我!
- 最后15分,小弟问个小问题?
{
string pictureExtension = System.IO.Path.GetExtension(fuPicture.FileName).ToLower();
string[] allowedExtension = { ".gif", ".png", ".jpeg", ".jpg" };
bool fileExtension = false;
for (int i = 0; i < allowedExtension.Length; i++)
{
if (pictureExtension == allowedExtension[i])
{
fileExtension = true;
}
}
if (fileExtension)
{
return fuPicture.FileName;
}
else
{
return null;
} }
if (fileName == null || fileName == string.Empty)
{
return "UPERROR";
}
另外LZ是从C/S转过来的吧,MessageBox.Show("只能上传相片!");之类的是不能在Web上用的,要用:
Response.Write("<script type='text/javascript'>alert('只能上传相片');</script>");
{
var fso, s = filespec;
fso = new ActiveXObject("Scripting.FileSystemObject");
if (fso.FileExists(filespec))
s += " exists.";
else
s += " doesn't exist.";
return(s);
}
{
alert('璇疯緭鍏ユ纭矾寰勶紒');
document.form1.all("FileUpload1").focus();
document.form1.all("FileUpload1").select();
return false;
}