日期:2014-05-17 浏览次数:20612 次
//文件上传按钮click事件
protected void btnFileUpload_Click(object sender, EventArgs e)
{
if (FileUpLoad1.HasFile)
{
//判断文件是否小于30k
if (FileUpLoad1.PostedFile.ContentLength < 30720)
{
try
{
String ad_file = "~/adimg/";//上传文件夹
//删除此广告位之前的图片
String[] files = Directory.GetFiles(Server.MapPath(ad_file));
foreach (string file in files)
{
if (Path.GetFileNameWithoutExtension(file) == "img"+adid)
{
File.Delete(file);
}
}
//上传新文件
FileUpLoad1.PostedFile.SaveAs(Server.MapPath(ad_file) + img_name(FileUpLoad1.FileName.ToString()));
lblMessage.Text = "上传成功!";
this.imgname.Value = img_name(FileUpLoad1.FileName.ToString());
}
catch (Exception ex)
{
lblMessage.Text += ex.Message;
}
}
else
{
lblMessage.Text = "上传文件不能大于30K!";
}
}
else
{
lblMessage.Text = "尚未选择文件!";
}
}
//修改文件上传文件名字
private String img_name(String str)
{
String Str = System.IO.Path.GetExtension(str);
return "img" + adid + Str;
}