上传图片后网页无法访问
源代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication11.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="文件上传" />
</div>
<asp:Image ID="Image1" runat="server" Height="125px" Width="161px" />
<p>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</p>
</form>
</body>
</html>
程序控件
namespace WebApplication11
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
bool filesValid = false;
if(this.FileUpload1.HasFile)
{
string fileExtension = System.IO.Path.GetExtension(this.FileUpload1.FileName).ToLower();
String[] restrictExtension = { "gif", "jpg", "bmp", "png" };
for(int i=0;i<restrictExtension.Length;i++)
{
if(fileExtension==restrictExtension[i])
{
filesValid = true;
}
if(filesValid==true)
{
try
{
this.Image1.ImageUrl = "-/images/" + FileUpload1.FileName;
this.FileUpload1.SaveAs(Server.MapPath("`/images/") + FileUpload1.FileName);
this.Label1.Text = "文件上传成功";
this.Label1.Text += "<Br/>";
this.Label1.Text += "<li>" + "文件路径:" + this.FileUpload1.PostedFile.FileName;
this.Label1.Text += "<Br/>";
this.Label1.Text += "<li>" + "文件大小:" + this.FileUpload1.PostedFile;
this.Label1.Text += "<Br/>";
this.Label1.Text += "<li>" + "文件类型:" + this.FileUpload1.PostedFile.ContentType;
}
catch
{
this.Label1.Text = "无法上传文件";
}
}
}
}
}
}
}