ASP.NET图像控件未显示最新上传的照片 [英] ASP.NET image control is not showing latest uploaded photo

查看:62
本文介绍了ASP.NET图像控件未显示最新上传的照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Page_Load中,我将图像分配给:

Hi,

In Page_Load, I am assigning an image to:

<asp:image id="imgPhoto" runat="server" xmlns:asp="#unknown"></asp:image>



这是我的代码:

page_load
===========



Here is my code:

page_load
============

string photo = siteurl + DBUtilities.ExecuteScalar(DBConnections.GetSQLConnectionString(), CommandType.Text, "select photo from visitors_images where visitor_id='" + Request.QueryString["visitor_no"].ToString() + "'").ToString();
            if (IsExists(photo))
                imgPhoto.ImageUrl = photo;
            else
                imgPhoto.ImageUrl = "~/includes/images/no_person.jpg";



Submit_button
============



Submit_button
=============

fulPhoto.SaveAs(Server.MapPath("~/includes/visitors/" + IMGNAME));
DBUtilities.ExecuteNonQuery(DBConnections.GetSQLConnectionString(), CommandType.Text, "UPDATE visitors_images SET photo='" + IMGNAME + "' WHERE visitor_id='" + Request.QueryString["visitor_no"].ToString() + "'");
Response.Redirect(Request.RawUrl.ToString());



但是,如果我手动刷新,它将显示最新照片.能否请您提出一些建议.

谢谢!



But, if I refresh manually, it shows the latest photo. Can you please give some suggestion.

Thanks!

推荐答案

好吧,您也许应该在ASP.NET上读一本书,对您的主题有所了解,然后再自称雇用我从事您的工作" ".您的问题不胜枚举,您的代码简直是一场灾难,由于您的网站不安全且编写得不好,因此我可以轻松地删除您的数据库.

您的核心问题是您不了解ASP.NET页面生命周期.您知道如何使用调试器吗?如果这样做了,并设置了一些断点,则会注意到在按钮事件之前页面加载正在运行,因此它加载了OLD照片,然后您的事件更改了照片,因此下次刷新时,您将看到新的那一个.如果您不能正确地编写此代码(并且如果不能,我希望没有可怜的傻瓜雇用"您来编写它),那么至少将其移至页面预渲染,这样就可以正常工作.
Well, you should perhaps read a book on ASP.NET, and know a bit about your subject before you call yourself ''hire me for your work''. Your issues are legion, your code is a disaster, and I could easy erase your database because your site is insecure and poorly written.

Your core issue is that you don''t understand the ASP.NET page lifecycle. Do you know how to use the debugger ? If you did, and you set some breakpoints, you''d notice that page load runs BEFORE your button event, so it loads the OLD photo, then your event changes the photo, so the next time you refresh, you''ll see the new one. If you can''t write this code properly ( and if you cannot, I hope no poor sucker has ''hired you'' to write it ), then at least move it to page prerender, and it will work fine.


你好,

我同意上面的帖子,当前代码对 SQL注入开放. [
hello,

I agree with the post above, the current code is open to SQL Injection.[^]

Here is a quick solution to bypass the browser cache, try append a random number in the query string. Make sure you fix the problems mentioned by Christian.

Random r = new Random();
        int RandomNumber = r.Next();



Response.Redirect(Request.RawUrl.ToString()+?r =" + RandomNumber.ToString();



Response.Redirect(Request.RawUrl.ToString()+"?r=" + RandomNumber.ToString();


为什么不设置

imgPhoto.ImageUrl

上传图像后.
意思是你做完之后
fulPhoto.SaveAs(Server.MapPath(〜/includes/visitors/" + IMGNAME));

这将确保正确的图像链接已传输到控件.

如果有完整的回发,并且您同步保存文件,则应显示该图像.不要使用任何线程来保存文件.
Why dont you set the

imgPhoto.ImageUrl

after you upload the image.
Means after you do
fulPhoto.SaveAs(Server.MapPath("~/includes/visitors/" + IMGNAME));

This will make sure that the proper image link is transferred to the control.

If there is a full postback, and you save the file synchronously, the image should be displayed. Dont use any thread to save the file.


这篇关于ASP.NET图像控件未显示最新上传的照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆