以前我都是判断扩展名,后来发现不太好.就找到了如下代码,但是本地可以运行.一传到服务器上就不行了.好像是需要什么权限吧?
服务器商又不给开权限(强烈鄙视咱们国家的垃圾服务器提供商),谁能给段代码验证一下文件类型啊?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;
} }
服务器商又不给开权限(强烈鄙视咱们国家的垃圾服务器提供商),谁能给段代码验证一下文件类型啊?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;
} }
解决方案 »
- ajax 页面局部更新变乱码,应该如何修正?
- 很多基础资料如何在一个页面中管理
- 给Repeater中每行数据加下划线
- 为什么会出现这个问题:The message could not be sent to the SMTP server. The transport error code w
- 明天就要答辩了,最后一个问题。大家帮帮忙。关于iframe的。
- 关于地图动态显示区域信息的问题?
- 我网站的论坛最近老是莫名其妙的卡,怀疑有人用登录器恶意登录,求解决方法
- datagrid中的dropdownlist变化引起其他cell值得变化,求解
- web页面a标签问题
- 超难问题,请高手近来解答
- 年后将制作一个大站,想学习一下网站架构,有没有这方面比较好的书
- asp.net怎么在GridView里数据加载前先加载DropDownList里的数据?
<input id="File1" type="file" style="width:240px" runat="server" name="files"/>
.cs:
string filetype = this.File1.PostedFile.ContentType;
读取前面几个字节用来效验。
byte[] buffer;
FileStream stream = new FileStream(FileName, FileMode.Open, FileAccess.Read);
stream.Read(buffer, 0, 7);循环比较一下buffer 和 HEADER是否一样。
if(this.FileUp.PostedFile.ContentType == "image/pjpeg" || this.FileUp.PostedFile.ContentType == "image/bmp" || this.FileUp.PostedFile.ContentType == "image/gif" || this.FileUp.PostedFile.ContentType == "image/x-png" && FileUp.PostedFile.ContentLength < 2 * 1024 * 1024)
{
this.FileUp.PostedFile.SaveAs(Server.MapPath("../UpFiles/") + "\\" + fileName);
this.UploadPan.Visible = false;
this.UploadedPan.Visible = true;
this.CopyTxt.Value = "../UpFiles/" + fileName;
}
else
{
this.message.Text = "文件格式不正确/或者文件过大!";
}