</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="Image" runat="server" width="50" Height="50"/>
<br />
<asp:TextBox ID="info" runat="server"></asp:TextBox>
<br />
<asp:FileUpload ID="up" runat="server" />
<br/>
<asp:Button ID="btnupLoad" runat="server" Text="Button"
onclick="btnupLoad_Click" />
</div>
</form>
</body>
</html>
后套代码:
protected void btnupLoad_Click(object sender, EventArgs e) { if (up.HasFile) { if (up.PostedFile.ContentType.Substring(0, 5) == "image") { try { string serverPath = Server.MapPath("upLoad"); if (!System.IO.Directory.Exists(serverPath)) { System.IO.Directory.CreateDirectory(serverPath); } string imgName = up.FileName; string newPath = serverPath + "\" + imgName; up.SaveAs(newPath); ClientScript.RegisterStartupScript(this.GetType(), "", "alert('上传成功');", true); info.Text = ""; Image.ImageUrl = "upLoad/" + imgName; info.Text += "路径:" + up.PostedFile.ContentType; } catch { ClientScript.RegisterStartupScript(this.GetType(), "", "alert('上传失败');", true); } } else { ClientScript.RegisterStartupScript(this.GetType(), "", "alert('请选择图片');", true); } } else { ClientScript.RegisterStartupScript(this.GetType(), "", "alert('选择要上传的图片');", true); } }
ClientScript.RegisterStartupScript(this.GetType(), "", "alert('请选择图片');", true);
若是gif图像,则ContentType的值为image/gif