我想在客户端对要上传的图片进行大小判断,用js是否可行.我在网上找了好多,都不行!
下面是我做的法,出来的值为-1;function CheckInput()
{ //alert($("#File1").val());return false;
var PicExt=$("#File1").val().substring($("#File1").val().lastIndexOf(".")+1);
if($("#<%=TextBox1.ClientID %>").val()==""){alert("請填寫季別清單!");return false;}
if($("#File1").val()!="")
{
if(PicExt!="png"&&PicExt!="PNG"){$("#File1").val("");alert("圖片格式不對!");return false;}
}
CheckImgSize($("#File1").val());
return false;
return true;
}
//检查图片大小
function CheckImgSize(ImgValue)
{ var oImg=new Image();
oImg.src=ImgValue;
oImg.onreadystatechange = function ()
{
if (oImg.readyState == "complete")
{
alert(oImg.fileSize);
}
}
alert(oImg.fileSize);
return false;
//if((oImg.fileSize)/1024).toFixed(1)>430) return true;
//else return false;
}
页面:<table>
<tr>
<td colspan="2" align="center" style="color:red">
*上傳的圖片后缀只能為.png的! 建议尺寸:320*460</td>
</tr>
<tr>
<td style="color:Black">
季别清单:
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Width="207px"></asp:TextBox>
</td>
</tr>
<tr>
<td style="color:Black">
背 景 圖:
</td>
<td>
<input id="File1" runat="server" type="file" style="width: 211px"/>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="提 交" OnClientClick="return CheckInput()" />
</td>
</tr>
</table>
下面是我做的法,出来的值为-1;function CheckInput()
{ //alert($("#File1").val());return false;
var PicExt=$("#File1").val().substring($("#File1").val().lastIndexOf(".")+1);
if($("#<%=TextBox1.ClientID %>").val()==""){alert("請填寫季別清單!");return false;}
if($("#File1").val()!="")
{
if(PicExt!="png"&&PicExt!="PNG"){$("#File1").val("");alert("圖片格式不對!");return false;}
}
CheckImgSize($("#File1").val());
return false;
return true;
}
//检查图片大小
function CheckImgSize(ImgValue)
{ var oImg=new Image();
oImg.src=ImgValue;
oImg.onreadystatechange = function ()
{
if (oImg.readyState == "complete")
{
alert(oImg.fileSize);
}
}
alert(oImg.fileSize);
return false;
//if((oImg.fileSize)/1024).toFixed(1)>430) return true;
//else return false;
}
页面:<table>
<tr>
<td colspan="2" align="center" style="color:red">
*上傳的圖片后缀只能為.png的! 建议尺寸:320*460</td>
</tr>
<tr>
<td style="color:Black">
季别清单:
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Width="207px"></asp:TextBox>
</td>
</tr>
<tr>
<td style="color:Black">
背 景 圖:
</td>
<td>
<input id="File1" runat="server" type="file" style="width: 211px"/>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="提 交" OnClientClick="return CheckInput()" />
</td>
</tr>
</table>
//验证文件大小
function ShowFolderSize(filespec)
{
var fso, f;
var maxsize=100;//定义允许文件的大小,单位B
fso = new ActiveXObject("Scripting.FileSystemObject");
if (fso.FolderExists(filespec)) {
f = fso.GetFolder(filespec);
} else if (fso.FileExists(filespec)) {
f = fso.GetFile(filespec);
} else {
alert("该文件不存在!");
return false;
}
if(f.size>maxsize)
{ alert( f.size);
alert("文件大小超出规定,请您选择小于"+maxsize+"字节的文件进行上传");
return false;
}
return true;
}
http://topic.csdn.net/u/20090106/10/857895a0-8291-422c-833f-a60a57b41f26.html
图片宽度:$("#myimg").width()
图片高度:$("#myimg").height()要注意一定要先显示出图片来,才能用这个获得图片大小,可以用document.write输出图片,然后再用。
string tpsize = (FileUpload1.PostedFile.ContentLength / 1024) + "KB";
以下是jsfunction CheckSize(ImgId,ErrorMsg,subBtId,MsgId)
{if($("#"+ImgId).val()=="") return false;
$("#"+MsgId).text("正在检查圖片大小,请稍等...");
$("#"+subBtId).attr("disabled",true);
$.post("CheckImgSize.ashx",{path:$("#"+ImgId).val(),timeStamp:new Date()},function(data){
if(data=="true"){$("#"+MsgId).text(ErrorMsg);CanSubmit+=1;}
else{$("#"+MsgId).text("");
if(CanSubmit!=0) CanSubmit-=1;if(CanSubmit==0) $("#"+subBtId).attr("disabled",false);}
}
后台处理文件:
CheckImgSize.ashx
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string Path = "";
try
{
Path = context.Request.Form["path"].ToString();
}
catch (Exception exc)
{
context.Response.Write("true");
context.Response.End();
}
CheckSize(Path, context);
}
/// <summary>
///
/// </summary>
/// <param name="fileNamePath"></param>
/// <param name="context"></param>
public void CheckSize(string fileNamePath,HttpContext context)
{
int AllowLength=400;
try
{
//要上传的文件
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
//BinaryReader r = new BinaryReader(fs);//二进制流进行读取
int ReallyLength=(int)fs.Length/1024;
fs.Close();
context.Response.Clear();
if (ReallyLength > AllowLength)
context.Response.Write("true");
else context.Response.Write("false");
}
catch(Exception ex)
{
context.Response.Write("false");
}
context.Response.End();
}