using System;
using System.IO;
namespace FileUpLoad
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_upload_Click(object sender, EventArgs e)
{
Boolean FileOK = false;
string path = Server.MapPath("~/uploadedImages/");//这个文件夹下
if (FileUpload1.HasFile == true)
{
string fileExtension = Path.GetExtension(FileUpload1.FileName).ToLower();//得到文件扩展名并转化为小写
string[] allowedExtension = { ".gif", ".png", ".jpg", ".jpeg" };
for (int i = 0; i < 4; i++)// i=allowedExtension.Length
{
if (fileExtension == allowedExtension[i])
{
FileOK = true;
}
}
}
if(FileOK)
{
try
{
FileUpload1.PostedFile.SaveAs(path+FileUpload1.FileName);//同样可以用 FileUpload1.SaveAs()进行保存
Label1.Text = "文件上传成功!";
}
catch(Exception ex)
{
Label1.Text="文件不能上传的原因为:"+ex.Message;
}
}
else
{
Label1.Text = "文件上传的格式不对!";
}
}
}
}