asp.net图片上传及删除
方法一:
UploadImage cetFU = new UploadImage(); if (cetFU.UpFile(FileUpload1, "teacher_picture"))//如果上传成功 第一个参数表示fileupload插件名称,第二表示文件夹的名称 { ImageName = cetFU.FileName; ImageType = ImageName.Substring(ImageName.IndexOf("."), ImageName.Length - ImageName.IndexOf(".")); } else//如果上传失败 { Response.Write(bc.MessageBox(cetFU.ErrMsg)); //弹出错误提示 }
能简单的就进行上传图片。
方法二:
protected void btnupload_Click(object sender, System.EventArgs e) { string fullFileName = this.File1.PostedFile.FileName; string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);获取文件名 string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1);获取文件类型 string time = this.dropyear.Text + this.droplist.Text; if (fileName.Length > 20) { Response.Write("<script language='javascript'>alert('您上传的文件名必须在15个字符之内!');</script>"); } else { if (type == "jpg" || type == "bmp" || type == "gif") { conn.Open(); string sql = "select * from kb where filepath='" + fileName + "'"; if (DB.ExecuteSqlEx(sql) == 1)//自己编写的类的方法,执行SQL语句 { this.File1.PostedFile.SaveAs(Server.MapPath("upload") + "\\" + fileName); sql = "insert into kb(filepath,kbtime) values('" + fileName + "','"+time+"')"; DB.ExecuteSql(sql); this.Image1.ImageUrl = "upload/" + fileName; this.Image1.Visible = true; sql = "select count(*) from kb"; SqlCommand cmd = new SqlCommand(sql, conn); this.lblNum.Text = cmd.ExecuteScalar().ToString(); Response.Write("<script language='javascript'>alert('上传成功,您可以继续上传其它图片!');</script>"); } else { Response.Write("<script language='javascript'>alert('图片名已存在,请重命名后再上传!');</script>"); } } else { Response.Write("<script language='javascript'>alert('你选择的图片格式错误!');</script>"); } } }
相对比较清晰的说明了上传的过程。
删除文件
File.Delete(Server.MapPath("teacher_picture\\" + ImageName));//删除某个文件