显示网页文件夹中的图像 [英] Displaying images from webpage folder
本文介绍了显示网页文件夹中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前陷入一项任务,该任务涉及显示网页文件夹中的图像.在数据库和网页文件夹中上传图像的代码运行良好.现在我需要在div标签中显示上传的图像.任何人都可以帮我弄清楚这个问题.
在此先感谢.
我的代码是这样的
aspx.cs
Hi,
I am currently stuck in a task which deals with displaying image from webpage folder. Code for the upload of the image in database and webpage folder is working good.Now I need to display the uploaded image in a div tag.Can anyone help me figuring this out.
thanks in advance.
My code goes like this
aspx.cs
public partial class Default3 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=ADMIN-PC;Initial Catalog= Official;Integrated Security=true;");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
string path = Server.MapPath("Image/");
if(FileUpload1.HasFile)
{
string ext=Path.GetExtension(FileUpload1.FileName);
if (ext== ".jpg" || ext== ".png")
{
FileUpload1.SaveAs(path+FileUpload1.FileName);
string name="~/Image/"+FileUpload1.FileName;
string s = "insert into immage values('"+TextBox1.Text.Trim()+"','"+name+"')";
SqlCommand cmd= new SqlCommand(s,con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Write("Your file has been uploaded");
}
else
{
Response.Write("You can upload only jpg and png files");
}
}
else
{
Response.Write("Please select a file");
}
}
}
aspx
aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Panel ID="panel1" runat="server">
<div>
<br />
<asp:Label ID="Label1" runat="server" Text="Name"
Visible="False">
<asp:TextBox ID="TextBox1" runat="server" Visible="False">
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
style="top: 345px; left: 641px; position: absolute; height: 26px; width: 56px"
Text="Submit" />
<div id="Imagediv">
style="width: 319px; removed 62px; removed 507px; removed: absolute; height: 275px">
<asp:FileUpload ID="FileUpload1" runat="server"
style="width: 217px; top: 239px; left: 60px; position: absolute; height: 22px" />
</div>
</div>
</form>
</body>
</html>
推荐答案
我认为最简单的方法是更新面板和图像控件.
这是我的建议:
1-将更新面板放置到页面上(以及脚本管理器)
2-将图像控件放入更新面板的内容占位符部分
3-完成数据库查询后,在"Button2_click"事件中设置图像控件的图像源
4-将更新面板上的触发器设置为仅在触发"Button2"上的按钮单击事件时进行更新
希望这会有所帮助,如果这种解释很清楚,我可能会为您提供示例代码,但这是我想到的最简单的方法.
I think the easiest way of doing this would be an update panel and an image control.
Here is what I propose:
1- Place an update panel onto the page (with a scriptmanager as well)
2- Place an image control into the update panel''s content place holder section
3- In the "Button2_click" event set the image source for the image control after you''ve done your database query
4- Set the triggers on the update panel to only update when the button click event on "Button2" is fired
Hope this helps and I can probably provide you with sample code if this explanation is clear enough, but this is the simplest way I think of achieving this.
这篇关于显示网页文件夹中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文