本文介绍了使用更新SQL语句时,FileUpload保留旧映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 你好所有 下面的代码工作正常,但我看的是如果用户没有选择新的照片那么代码将不会更新当前图片。下面的代码,当我点击更新按钮(并保持文件上传为空)它没有显示图像,但目前有一个旧图像。 < asp:FormView ID = EditUsrInfoFVw runat = server > < ItemTemplate > < tr > & td > 名字: < / td > < td > < ; asp:TextBox ID = FNEDTTXTBX runat = server CssClass = 表单-control Tex t =' <% #Bind ( FN)%>' > < / asp:TextBox > < / td > ; < / tr > < tr > < td > 姓氏:< / td > < td > < asp:TextBox ID = LNEDTTXTBX runat = server CssClass = form-control 文本 =' <% #Bind( LN)%>' > < / asp:TextBox > < / td > < / tr > < tr > < tr > < td > 个人图片:< / td > < td > < asp:FileUpload ID = EditImgFUP runat = server CssClass = 表单控制 / > < / td > < / tr > < / ItemTemplate > < / asp:FormView > protected void editHPPersInfobtn_Command(对象 sender,CommandEventArgs e) { EditUsrPan.Visible = false ; showusrinfo.Visible = true ; SqlConnection EduSRiNFOCon = new SqlConnection(sc); SqlCommand EditUsrInfocmd = new SqlCommand(); if (会话[ UsrNme]!= null ) { string EditUsrInfoSQL = @ 更新UserInfo SET FN = @ FN,LN = @ LN,密码= @密码,RePass = @ RePass,网站= @网站,发布= @发布,电子邮件= @电子邮件,地址= @地址,TeleNum = @ TeleNum,Facebook = @ Facebook,GooglePlus = @ GooglePlus,Twitter = @Twitter,Img = @ img其中UID = @ UID; EditUsrInfocmd.Connection = EduSRiNFOCon; EditUsrInfocmd.CommandType = CommandType.Text; EditUsrInfocmd.CommandText = EditUsrInfoSQL; EditUsrInfocmd.Parameters.AddWithValue( @ UID,((Label)EditUsrInfoFVw.FindControl ( UIDEDLabel))。文字); EditUsrInfocmd.Parameters.AddWithValue( @ TeleNum,((TextBox)EditUsrInfoFVw.FindControl ( TeleEDTTXTBX))。文本); EditUsrInfocmd.Parameters.AddWithValue( @ Facebook,((TextBox)EditUsrInfoFVw.FindControl ( FaceEDTTXTBX))。Text); EditUsrInfocmd.Parameters.AddWithValue( @ GooglePlus,((TextBox)EditUsrInfoFVw.FindControl ( GoogEDTTXTBX))。文字); EditUsrInfocmd.Parameters.AddWithValue( @ Twitter,((TextBox)EditUsrInfoFVw.FindControl ( TwitEDTTXTBX))。Text); EditUsrInfocmd.Parameters.AddWithValue( @ img,((FileUpload)EditUsrInfoFVw.FindControl ( EditImgFUP))。FileName); var fileUpload = EditUsrInfoFVw.FindControl( EditImgFUP) as FileUpload; if (fileUpload!= null && fileUpload.HasFile) { if (fileUpload.PostedFile.ContentType.ToLower()。StartsWith( image /)) { fileUpload.SaveAs(Server.MapPath( 〜/ images / users / + fileUpload.FileName)); } 其他 { // 不是图像,相应处理 } } EduSRiNFOCon.Open(); int result = EditUsrInfocmd.ExecuteNonQuery(); if (结果> 0 ) { // 已成功更新; } } ReHPPersInfo(); } 解决方案 你的问题是在用户没有选择图像的时候, 你的代码是 if (fileUpload!= null && fileUpload.HasFile) { if (fileUpload.PostedFile.ContentType.ToLower()。StartsWith( image /)) { fileUpload.SaveAs(Server.MapPath( 〜/ images / users / + fileUpload.FileName)); } 其他 { // 不 image ,相应处理} } 因此用户必须选择 图像 执行 上述条件。 if中的else(fileUpload!= null& ;& fileUpload.HasFile)完全没用。 简单的U可以理解你需要两个查询一个用于图像另一个没有图像。你写的查询只在用户选择图像时执行.. 小钥匙 if(fileupload) .HasFile) { 查询图片更新 } 其他 { 查询无图像更新 } if (会话[ UsrNme]!= null ) { string EditUsrInfoSQL = @ 更新UserInfo SET FN = @ FN,LN = @ LN,密码= @密码,RePass = @ RePass,网站= @网站,发布= @发布,电子邮件= @电子邮件,地址= @地址,TeleNum = @ TeleNum, Facebook = @ Facebook,GooglePlus = @ GooglePlus,Twitter = @Twitter,Img = @ img其中UID = @ UID; string EditUsrInfoSQLWithoutImage = @ 更新UserInfo SET FN = @ FN,LN = @ LN,密码= @密码,RePass = @ RePass,网站= @ Website,Post = @ Post,Email = @ Email, Address = @ Address,TeleNum = @ TeleNum,Facebook = @Facebook,GooglePlus = @ GooglePlus,Twitter = @ Twitter其中UID = @ UID; // img = @ img不使用 EditUsrInfocmd.Connection = EduSRiNFOCon; EditUsrInfocmd.CommandType = CommandType.Text; EditUsrInfocmd.CommandText = EditUsrInfoSQL; EditUsrInfocmd.Parameters.AddWithValue(& quot; @UID& quot;,((Label)EditUsrInfoFVw.FindControl(& quot; UIDEDLabel& quot;))。Text); EditUsrInfocmd.Parameters.AddWithValue(& quot; @ TeleNum& quot;,((TextBox)EditUsrInfoFVw.FindControl(& quot; TeleEDTTXTBX& quot;))。Text); EditUsrInfocmd.Parameters.AddWithValue(& quot; @Face& quot;,((TextBox)EditUsrInfoFVw.FindControl(& quot; FaceEDTTXTBX& quot;))。Text); EditUsrInfocmd.Parameters.AddWithValue(& quot; @ GooglePlus& quot;,((TextBox)EditUsrInfoFVw.FindControl(& quot; GoogEDTTXTBX& quot;))。Text); EditUsrInfocmd.Parameters.AddWithValue(& quot; @ Twitter& quot;,((TextBox)EditUsrInfoFVw.FindControl(& quot; TwitEDTTXTBX& quot;))。Text); EditUsrInfocmd.Parameters.AddWithValue(& quot; @ img& quot;,((FileUpload)EditUsrInfoFVw.FindControl(& quot; EditImgFUP& quot;))。FileName); var fileUpload = EditUsrInfoFVw.FindControl(& quot; EditImgFUP& quot;) as FileUpload; if (fileUpload!= null & amp;& amp; fileUpload.HasFile) { fileUpload.SaveAs(Server.MapPath(& quot;〜/ images / users /& quot; + fileUpload.FileName)); EduSRiNFOCon.Open(); int result = EditUsrInfocmd.ExecuteNonQuery(); if (结果> 0 ) { // 已成功更新; } } else { EduSRiNFOCon.Open(); int result = EditUsrInfoSQLWithoutImage .ExecuteNonQuery(); if (结果> 0 ) { // 已成功更新; } } } } ReHPPersInfo(); } 如果旧图片删除 string imageFilePath = Serve.MapPath( @ 〜/ uploaded / imagefilename.extension); System.IO.File.Delete( imageFilePath); Hello allThe below code working fine, but i am looking in case if the user did not choose a new photo then the code will not update the current image. The below code when i click on the update button (and keep the file upload empty) it show no image but currently there are an old image. <asp:FormView ID="EditUsrInfoFVw" runat="server"> <ItemTemplate> <tr> <td>First Name:</td> <td><asp:TextBox ID="FNEDTTXTBX" runat="server" CssClass="form-control" Text='<%# Bind("FN") %>'></asp:TextBox></td> </tr> <tr> <td>Last Name:</td> <td> <asp:TextBox ID="LNEDTTXTBX" runat="server" CssClass="form-control" Text='<%# Bind("LN") %>'> </asp:TextBox></td> </tr> <tr><tr> <td>Personal Image:</td> <td> <asp:FileUpload ID="EditImgFUP" runat="server" CssClass="form-control" /> </td> </tr></ItemTemplate> </asp:FormView> protected void editHPPersInfobtn_Command(object sender, CommandEventArgs e) { EditUsrPan.Visible = false; showusrinfo.Visible = true; SqlConnection EduSRiNFOCon = new SqlConnection(sc); SqlCommand EditUsrInfocmd = new SqlCommand(); if (Session["UsrNme"] != null) { string EditUsrInfoSQL = @"Update UserInfo SET FN=@FN, LN=@LN, Password=@Password, RePass=@RePass, Website=@Website, Post=@Post, Email=@Email,Address=@Address, TeleNum=@TeleNum, Facebook=@Facebook, GooglePlus=@GooglePlus, Twitter=@Twitter, Img=@img Where UID=@UID"; EditUsrInfocmd.Connection = EduSRiNFOCon; EditUsrInfocmd.CommandType = CommandType.Text; EditUsrInfocmd.CommandText = EditUsrInfoSQL; EditUsrInfocmd.Parameters.AddWithValue("@UID", ((Label)EditUsrInfoFVw.FindControl("UIDEDLabel")).Text); EditUsrInfocmd.Parameters.AddWithValue("@TeleNum", ((TextBox)EditUsrInfoFVw.FindControl("TeleEDTTXTBX")).Text); EditUsrInfocmd.Parameters.AddWithValue("@Facebook", ((TextBox)EditUsrInfoFVw.FindControl("FaceEDTTXTBX")).Text); EditUsrInfocmd.Parameters.AddWithValue("@GooglePlus", ((TextBox)EditUsrInfoFVw.FindControl("GoogEDTTXTBX")).Text); EditUsrInfocmd.Parameters.AddWithValue("@Twitter", ((TextBox)EditUsrInfoFVw.FindControl("TwitEDTTXTBX")).Text); EditUsrInfocmd.Parameters.AddWithValue("@img", ((FileUpload)EditUsrInfoFVw.FindControl("EditImgFUP")).FileName); var fileUpload = EditUsrInfoFVw.FindControl("EditImgFUP") as FileUpload; if (fileUpload != null && fileUpload.HasFile) { if (fileUpload.PostedFile.ContentType.ToLower().StartsWith("image/")) { fileUpload.SaveAs(Server.MapPath("~/images/users/" + fileUpload.FileName)); } else { // Not an image, handle accordingly } } EduSRiNFOCon.Open(); int result = EditUsrInfocmd.ExecuteNonQuery(); if (result > 0) { // Updated successfully; } } ReHPPersInfo(); } 解决方案 Your problem is while the user doesn't choose a image,yours code isif (fileUpload != null && fileUpload.HasFile) { if (fileUpload.PostedFile.ContentType.ToLower().StartsWith("image/")) { fileUpload.SaveAs(Server.MapPath("~/images/users/" + fileUpload.FileName)); } else { // Not an image, handle accordingly } }hence the user must select an image to execute on the above condition. the the else in the if (fileUpload != null && fileUpload.HasFile) is totally useless.Simply U can understand mean u need two query one is for with image another for without image .the query u written only execute when the user select an image.. small key if(fileupload.HasFile){query with image update}else{query without image update}if (Session["UsrNme"] != null) { string EditUsrInfoSQL = @"Update UserInfo SET FN=@FN, LN=@LN, Password=@Password, RePass=@RePass, Website=@Website, Post=@Post, Email=@Email,Address=@Address, TeleNum=@TeleNum, Facebook=@Facebook, GooglePlus=@GooglePlus, Twitter=@Twitter, Img=@img Where UID=@UID"; string EditUsrInfoSQLWithoutImage = @"Update UserInfo SET FN=@FN, LN=@LN, Password=@Password, RePass=@RePass, Website=@Website, Post=@Post, Email=@Email,Address=@Address, TeleNum=@TeleNum, Facebook=@Facebook, GooglePlus=@GooglePlus, Twitter=@Twitter Where UID=@UID"; //img=@img not to be use EditUsrInfocmd.Connection = EduSRiNFOCon; EditUsrInfocmd.CommandType = CommandType.Text; EditUsrInfocmd.CommandText = EditUsrInfoSQL; EditUsrInfocmd.Parameters.AddWithValue(&quot;@UID&quot;, ((Label)EditUsrInfoFVw.FindControl(&quot;UIDEDLabel&quot;)).Text); EditUsrInfocmd.Parameters.AddWithValue(&quot;@TeleNum&quot;, ((TextBox)EditUsrInfoFVw.FindControl(&quot;TeleEDTTXTBX&quot;)).Text); EditUsrInfocmd.Parameters.AddWithValue(&quot;@Facebook&quot;, ((TextBox)EditUsrInfoFVw.FindControl(&quot;FaceEDTTXTBX&quot;)).Text); EditUsrInfocmd.Parameters.AddWithValue(&quot;@GooglePlus&quot;, ((TextBox)EditUsrInfoFVw.FindControl(&quot;GoogEDTTXTBX&quot;)).Text); EditUsrInfocmd.Parameters.AddWithValue(&quot;@Twitter&quot;, ((TextBox)EditUsrInfoFVw.FindControl(&quot;TwitEDTTXTBX&quot;)).Text); EditUsrInfocmd.Parameters.AddWithValue(&quot;@img&quot;, ((FileUpload)EditUsrInfoFVw.FindControl(&quot;EditImgFUP&quot;)).FileName); var fileUpload = EditUsrInfoFVw.FindControl(&quot;EditImgFUP&quot;) as FileUpload; if (fileUpload != null &amp;&amp; fileUpload.HasFile) { fileUpload.SaveAs(Server.MapPath(&quot;~/images/users/&quot; + fileUpload.FileName)); EduSRiNFOCon.Open(); int result = EditUsrInfocmd.ExecuteNonQuery(); if (result > 0) { // Updated successfully; } } else { EduSRiNFOCon.Open(); int result = EditUsrInfoSQLWithoutImage.ExecuteNonQuery(); if (result > 0) { // Updated successfully; } } } } ReHPPersInfo(); }if old image delete string imageFilePath = Serve.MapPath(@"~/uploaded/imagefilename.extension");System.IO.File.Delete("imageFilePath"); 这篇关于使用更新SQL语句时,FileUpload保留旧映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 06:08