.net的图片上传SQL数据库的问题!
这段代码没有任何毛病!但是我现在需要将里面向数据库存储的那部分代码提取出来放到if外面需要怎么做!!!谢谢大家!!没有分了!没有悬赏!对不起大家!if(FileUploa...
这段代码没有任何毛病!但是我现在需要将里面向数据库存储的那部分代码提取出来 放到if外面 需要怎么做 !!!谢谢大家!! 没有分了!没有悬赏!对不起大家!
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>");
} 展开
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>");
} 展开
1个回答
展开全部
你的代码耦合性太强了,按你的想法改了一下,但是你这种编码方法不太好
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 = 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();
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 = 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();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询