fileupload控件上传文件
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Boolean fileOK = false;
String path = Server.MapPath("192.168.0.222/root/");
if (file1.HasFile)
{
String fileExtension =
System.IO.Path.GetExtension(file1.FileName).ToLower();
String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
}
if (fileOK)
{
try
{
file1.PostedFile.SaveAs(path
+ file1.FileName);
lbcg.Text = "File uploaded!";
}
catch (Exception ex)
{
lbcg.Text = "文件无法上传.";
}
}
else
{
lbcg.Text = "系统不能接受这种类型的文件!";
}
}
}