您的位置:首页 > 其它

注册页面上传图片的两种方法。

2013-03-14 20:08 375 查看
两种方法:

if(FileUpload1.FileName !="")

{

string ImgName =FileUpload1.PostedFile.FileName;

string ImgExtention =System.IO.Path.GetExtension(ImgName);

int ImgSize =FileUpload1.PostedFile.ContentLength;

string newname =DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() +DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() +DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();

string ImgPath =Server.MapPath("~/Ftb/");

string ImgUrl ="~/Ftb/" + newname + ImgExtention;

if (ImgExtention ==".gif" || ImgExtention == ".GIF" || ImgExtention ==".jpg" || ImgExtention == ".JPG" || ImgExtention ==".jpeg" || ImgExtention == ".JPEG")

{

//图片尺寸

if (ImgSize / 512000< 1)

{

//数据添加

//DateTime now =DateTime.Now;

FileUpload1.PostedFile.SaveAs(ImgPath+ newname + ImgExtention);

string constr =ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;

SqlConnection con= new SqlConnection(constr);

con.Open();

DateTime itime =DateTime.Now;

string str ="insert into picture(txt,time) values('" + ImgUrl +"','"+itime+"')";//Imgurl就是图片的路径 上面定义的

SqlCommand cmd =new SqlCommand(str, con);

int i =cmd.ExecuteNonQuery();

if (i > 0)

{

Response.Write("<script>alert('添加成功')</script>");

}

else

{

Response.Write("<script>alert('添加失败')</script>");

}

con.Close();

}

else

{

ClientScript.RegisterStartupScript(this.GetType(), "","<script>alert('图片大小不能超过1M!');</script>");

}

}

else

{

ClientScript.RegisterStartupScript(this.GetType(), "","<script>alert('图片格式不正确,只支持.gif .jpg .jpeg格式类型的图片!');</script>");

}

}

else

{

ClientScript.RegisterStartupScript(this.GetType(), "","<script>alert('请选择要上传的图片!');</script>");

}

======================================================================

方法2:

if(string.IsNullOrEmpty(FileUpload1.FileName))

{

ClientScript.RegisterStartupScript(this.GetType(),"", "<script>alert('请选择要上传的图片!');</script>");

return;

}

string ImgName =FileUpload1.PostedFile.FileName;

string ImgExtention =System.IO.Path.GetExtension(ImgName);

int ImgSize =FileUpload1.PostedFile.ContentLength;

string newname =DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() +DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() +DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();

string ImgPath =Server.MapPath("~/Ftb/");

string ImgUrl ="~/Ftb/" + newname + ImgExtention;

if (ImgExtention !=".gif" && ImgExtention != ".GIF" &&ImgExtention != ".jpg" && ImgExtention != ".JPG"&& ImgExtention != ".jpeg" && ImgExtention !=".JPEG")

{

ClientScript.RegisterStartupScript(this.GetType(), "","<script>alert('图片格式不正确,只支持.gif .jpg .jpeg格式类型的图片!');</script>");

return;

}

//图片尺寸

if (ImgSize / 512000 >= 1)

{

ClientScript.RegisterStartupScript(this.GetType(), "","<script>alert('图片大小不能超过1M!');</script>");

return;

}

//数据添加

//DateTime now =DateTime.Now;

FileUpload1.PostedFile.SaveAs(ImgPath + newname + ImgExtention);

string constr =ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;

SqlConnection con = newSqlConnection(constr);

con.Open();

DateTime itime =DateTime.Now;

string str = "insertinto picture(txt,time) values('" + ImgUrl + "','" + itime +"')";//Imgurl就是图片的路径 上面定义的

SqlCommand cmd = newSqlCommand(str, con);

int i =cmd.ExecuteNonQuery();

if (i > 0)

{

Response.Write("<script>alert('添加成功')</script>");

}

else

{

Response.Write("<script>alert('添加失败')</script>");

}

con.Close();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐