我想限制上传文件的类型:
<input type="file" id="MyFileInput" accept="image/jpeg" runat="server">
这个accept属性怎么不管用?而且在VS里还是用红色下划线标出,说是没有这个属性。我在IIS里设了MIME类型也不管用
<input type="file" id="MyFileInput" accept="image/jpeg" runat="server">
这个accept属性怎么不管用?而且在VS里还是用红色下划线标出,说是没有这个属性。我在IIS里设了MIME类型也不管用
<head>
<title>客户端控制上传图像大小示例</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<p>以下是一个客户端控制file上传图像大小的一个示例,由于是客户端限制,所以用户可以绕过这个限制。</p>
<SCRIPT LANGUAGE="JavaScript">
<!--
var FileMaxSize = 50;//限制上传的文件大小,单位(k)
function chkform(){
if (document.HwForm.Photo.value==""){alert("请选择照片");document.HwForm.Photo.focus();return false;}
if(document.HwForm.PhotoFileSize.value<=0){alert("请重新选择照片");document.HwForm.Photo.focus();return false;}
if(document.HwForm.PhotoFileSize.value>FileMaxSize*1024){alert("选择的照片超过"+FileMaxSize+"K,请重新选择");document.HwForm.Photo.focus();return false;}
alert("可以提交,当前为测试...");return false;
}
//-->
</SCRIPT>
<form method="POST" name="HwForm" onsubmit="return(chkform());" enctype="multipart/form-data">
照片(请选择一个50K以内的图片文件):<input type="file" name="Photo" onpropertychange="if(document.HwForm.Photo.value!=''){document.getElementById('PhotoImg').src=document.HwForm.Photo.value;}" /><IMG style="POSITION: absolute; TOP: -100000px;left:-100000px;" onerror="document.all.PhotoFileSize.value='-1';alert('请选择一个图像文件');"
onload="if(document.getElementById('PhotoImg').fileSize<=1024*FileMaxSize){document.all.PhotoFileSize.value=document.getElementById('PhotoImg').fileSize;}else{alert('图片不能大于'+FileMaxSize+'K,请重新选择');document.all.PhotoFileSize.value='-1';}" id="PhotoImg"><input size="3" type="hidden" name="PhotoFileSize" value="-1" />
<input type="submit" value="提交"> <input type="reset" value="重置">
</form>
</body>
</html>
string imageType=imagePath.Substring(imagePath.LastIndexOf(".")+1);
if (imageType.ToString()=="jpg" || imageType.ToString()=="gif")
{
.....
}用这个判断
if( file2.value.indexOf(".pdf",0)=="-1"){
alert("您所選取的檔案格式錯誤!!");
document.form1.all.myfile2.innerHTML = "<INPUT name='file2' type='file' size=50 onchange='checkpdf(this)'>"; }
}<SPAN id="myfile2">
<INPUT name='file2' type='file' size=50 onchange="checkpdf(this)">
</SPAN>
自己打印出来就知道文件类型是什么了,用这个方法即使是修改了扩展名也可以判断
HttpPostedFile PostedFile = Request.Files[0];
fileExt = (System.IO.Path.GetExtension(PostedFile.FileName)).ToString().ToLower();
if(!((fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".png" || fileExt == ".swf")))
{
ErrorMessage="只有jpg,gif,png,swf四种格式上传。";
return ErrorMessage;
}
Response.Write("只能上传jpg,gif,bmp,png,jpeg五种格式的图片!")
Response.End
End If
<script>function checkFile()
{
if (form3.file1.value=="")
{
alert("请上传!!");
return false;
}var typeValue = form3.file1.value;
var type = typeValue.substring(typeValue.lastIndexOf("\\")+1,typeValue.length);
type = type.substring(type.lastIndexOf("\.")+1,type.length)
if (type.toUpperCase()=="GIF" || type.toUpperCase()=="JPG" || type.toUpperCase()=="PNG"){
alert("ok");
return true;
}
else
{
alert("请检查文件是否正确!");
return false;
}}
</script>
同意下面的方法:
___________________________________________
回复人: GDNPC(烟锁池塘柳,炮镇海城楼) ( ) 信誉:100 2005-09-14 09:05:00 得分: 0
FileUpLoad.PostedFile.ContentType.ToString();
自己打印出来就知道文件类型是什么了,用这个方法即使是修改了扩展名也可以判断
___________________________________________
用lastindexof取出.后面的后缀进行比较
同意用这个