问题陈述:我要通过点击
<input id="loFile"  type="file" size="25"runat="server" height="100">浏览,后弹出的对框中,只显示图片文件
望高手指点,
最好是给点代码,谢谢

解决方案 »

  1.   

    只能在提交的时候,用JavaScript判断File控件的.value的后缀名
    不是图片文件,则返回false并提示
      

  2.   

    <script language="javascript">
    function check(obj)
    {
    var path = obj.value;
    var fileType = path.substring(path.lastIndexOf('.'),path.length).toUpperCase();

    if(fileType!=".JPG" && fileType!=".GIF" && fileType!=".BMP")
    alert(fileType + " is not a type of image");
    else
    document.getElementById("Image1").src = path;
    }
    </script>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:Image id="Image1" runat="server"></asp:Image>
    <INPUT id="File1" type="file" name="File1" runat="server" onchange="check(this);">
    <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
    </form>
    </body>
      

  3.   

    CS代码:
    if(inputFile.PostedFile.ContentLength > 0) 
    {
    //设定上传文件的保存路径

    string strSaveDir = "images/";
    string strName = inputFile.PostedFile.FileName;

    //取得文件名(抱括路径)里最后一个"."的索引
    int intExt = strName.LastIndexOf(".");

    //取得文件扩展名
    string strExt = strName.Substring(intExt);
    strExt = strExt.ToLower();

    if((strExt==".jpg") ||( strExt==".jpeg") || (strExt==".gif"))
    {
    //这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
    if((inputFile.PostedFile.ContentLength / 1024) >60)
    {
    Response.Write("<script>alert('图片大小不能超过100K!');</script>");
    return;
    }
    DateTime datNow = DateTime.Now; 
    string strNewName = datNow.Year.ToString() +datNow.Month.ToString()+datNow.Day.ToString()+datNow.Hour.ToString()+datNow.Minute.ToString()+datNow.Second.ToString() ; 
    strNewName=strNewName+strExt; //取得文件名(包括路径)里最后一个"\"的索引
    int intPath = strName.LastIndexOf("\\"); //取得文件名(不包括路径)
    //string strNewName = strName.Substring(intPath); 
    inputFile.PostedFile.SaveAs(Server.MapPath(strSaveDir + strNewName)); 
    //得到这个文件的相关属性:文件名,文件类型,文件大小
    Response.Write("<script>alert('上传成功!');</script>");
    Session["IsLoad"] = true;
    Session["pic"] =  strNewName;

    //labelFileExt.Text = "文件类型:" + inputFile.PostedFile.ContentType + "( " + strExt + " )";
    //labelFileSize.Text = "文件大小:" + (inputFile.PostedFile.ContentLength / 1024).ToString() + " K Byte(s)"; }
    else
    {
    Response.Write("<script>alert('只能上传格式为:jpg,jpeg,gif 的图片!');location.href=\"liveshow_add.aspx\";</script>");
    }

    else 
    {
    Response.Write("<script>alert('请选择要上传的图片!');</script>");
    }
      

  4.   

    System.IO.Path.GetExtension(CmdFile.PostedFile.FileName);