private void btn_Upload_Click(object sender, System.EventArgs e)
{
//上传图片的程序段
DateTime now = DateTime.Now ;
//取现在时间到DataTime类的对象now中
string strBaseLocation=ConfigurationSettings.AppSettings["upimages"];
//这是文件将上传到的服务器的绝对目录
bool jpg = file1.Value.ToLower().EndsWith("jpg");
bool gif = file1.Value.ToLower().EndsWith("gif");
if (jpg == false && gif == false)
{
Response.Write("<script>alert('只能上传jpg和gif格式文件');</script>");
return;
}
if (file1.PostedFile.ContentLength>3072)
{
Response.Write("<script>alert('图片大小不能大于3K');</script>");
return;
}
string path_name="";
string filename="";
if (jpg == true)
{
path_name=strBaseLocation+@"\"+now.Hour.ToString()+now.Minute.ToString()+now.Second.ToString()+now.Millisecond.ToString()+file1.PostedFile.ContentLength.ToString()+".jpg";
filename=now.Hour.ToString()+now.Minute.ToString()+now.Second.ToString()+now.Millisecond.ToString()+file1.PostedFile.ContentLength.ToString()+".jpg";
}
if (gif == true)
{
path_name=strBaseLocation+@"\"+now.Hour.ToString()+now.Minute.ToString()+now.Second.ToString()+now.Millisecond.ToString()+file1.PostedFile.ContentLength.ToString()+".gif";
filename=now.Hour.ToString()+now.Minute.ToString()+now.Second.ToString()+now.Millisecond.ToString()+file1.PostedFile.ContentLength.ToString()+".gif";
}
if (file1.PostedFile.ContentLength != 0) //判断选取对话框选取的文件长度是否为0
{
try
{
bool havelogo = mc.checklogoname(filename);
if (havelogo == true)
{
Response.Write("<script>alert('服务器忙,请稍候再试');</script>");
return;
}
file1.PostedFile.SaveAs(path_name); //执行上传
string oldlogo = mc.updatelogo(_EditUser,filename);
if (oldlogo != string.Empty)
{
//删除原来的LOGO文件
string delFile =
strBaseLocation+@"\"+oldlogo;
delFile=delFile.Replace("/",@"\");
delFile=delFile.Replace(@"
\\",@"\");
File.Delete(delFile);
}
img_Logo.ImageUrl="/upimages/"+filename;
img_Logo.Visible=true;
Response.Write("<script>alert('上传成功');</script>");
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
else
{
Response.Write("<script>alert('请选择您要上传的图片文件');</script>");
return;
}
}