注册页面上传图片的两种方法。
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();
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();
相关文章推荐
- 两种php实现图片上传的方法
- 史上最全的CSS hack方式一览 jQuery 图片轮播的代码分离 JQuery中的动画 C#中Trim()、TrimStart()、TrimEnd()的用法 marquee 标签的使用详情 js鼠标事件 js添加遮罩层 页面上通过地址栏传值时出现乱码的两种解决方法 ref和out的区别在c#中 总结
- php fckeditor 上传文件(或图片)中文显示为乱码的解决方法(两种方法)
- web自动化测试图片上传的两种方法
- 在ASP.NET页面中显示自动生成图片的两种方法
- 两种php实现图片上传的方法
- iPhone UI设计注册页面的实现及TextField关闭键盘的两种方法
- 完美解决x2注册页面出现‘抱歉,你输入的用户名小于3个字符问题’的两种方法
- tornado web高级开发项目之抽屉官网的页面登陆验证、form验证、点赞、评论、文章分页处理、发送邮箱验证码、登陆验证码、注册、发布文章、上传图片
- 完美解决x2注册页面出现‘抱歉,你输入的用户名小于3个字符问题’的两种方法
- vue中使用axios post上传头像/图片并实时显示到页面的方法
- 两种方法实现点击图片调用上传功能
- SpringBoot限制文件或图片上传大小的两种配置方法
- iOS实现图片压缩的两种方法及图片压缩上传功能
- 在C#.net中做页面上传的程序。用Dhtml的控件:(创建文件上载控件,该控件带有一个文本框和一个浏览按钮。)和类HtmlInputFile的两种方法
- iPhone UI设计注册页面的实现及TextField关闭键盘的两种方法
- Servlet doPost方法同时上传图片和传递参数
- 两种方法上传本地文件到github
- 在jsp页面中添加富文本编译器(ueditor)+ 图片上传功能
- ie9后浏览器fckeditor无法上传图片、弹出浮层内容不显示的解决方法