asp.net2.0实现文件上传,控制上传文件大小,类型
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
Label2.Text = FileUpload1.FileName;
Label3.Text = FileUpload1.PostedFile.ContentLength.ToString() + "字节";
Label4.Text = FileUpload1.PostedFile.FileName;
//获得扩展名
string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
if (extension == ".rar" || extension == ".doc" || extension == ".xls")
{
if (FileUpload1.PostedFile.ContentLength < 10000000)//文件小于10M
{
FileUpload1.SaveAs(Server.MapPath("~/upLoad/" + FileUpload1.FileName));
Label1.Text = "恭喜你,上传成功!";
}
else
{
Label1.Text = "上传失败,不是管理员,上传文件不能大于10M!";
}
}
else
Label1.Text = "上传失败,只能上传扩展名是rar、doc、xls的文件!";
}
else
{
Label1.Text = "上传失败,或没指定正确的文件!";
}
}