我有一个包含3个表格的数据库,其中一个表格已经上传了来自用户的图像,每个表格都有一个个人资料页面。我已经设法在登录和搜索中显示带有gridview的图片,但它并不是非常令人愉悦。我决定遵循asp:image方法:)
我现在无法做的是显示用户登录时引用的图片或搜索后显示的用户个人资料。
所以,如果我找到一种方法将id传递给处理程序?要么...?
感谢您的帮助!
Default.aspx.cs(在Imageupload()和ImagebindGrid()中发生的事情我绕过了gridview代码,所以我可以尝试用其他方式显示它。
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Security.Principal;
using System.Data.Common;
namespace DisplayingImages
{
public partial class Default : System.Web.UI.Page
{
public string query, constr, query1, query2, query3;
public SqlConnection con, conn;
public void connection()
{
constr = ConfigurationManager.ConnectionStrings["Myconnection"].ToString();
con = new SqlConnection(constr);
con.Open();
}
protected void Page_Load(object sender, EventArgs e)
{
Label1.Visible = false;
if (!IsPostBack)
{
EM = (string)(Session["Email_Account"]);
lblemail.Text = EM;
SN = Convert.ToString(Session["Surname"]);
lblname.Text = SN;
PS = (string)(Session["Password"]);
PID = (int)(Session["id"]);
HttpContext context = HttpContext.Current;
context.Session["Email_Account"] = EM;
EM = (string)(context.Session["Email_Account"]);
if (PID == null)
{
Response.Redirect("Login.aspx");
}
imagebindGrid();
PostSelection();
}
}
private void InitializeComponent()
{
throw new NotImplementedException();
}
protected void upload(object sender, EventArgs e) // button για το upload της φωτο. Καλειται η κλαση Imageupload()
{
Imageupload();
}
/* Κλασση για upload εικονας . Ελεγχος και περασμα εικονας */
private void Imageupload()
{
if (FileUpload1.HasFile)
{
PID = (int)(Session["id"]);
if (PID != null)
{
int imagefilelenth = FileUpload1.PostedFile.ContentLength;
byte[] imgarray = new byte[imagefilelenth];
HttpPostedFile image = FileUpload1.PostedFile;
image.InputStream.Read(imgarray, 0, imagefilelenth);
connection();
query = "Insert into ImageToDB (user_id,ImageName,Image) values (@user_id,@Name,@Image)";
SqlCommand com = new SqlCommand(query, con);
com.Parameters.AddWithValue("@Name", SqlDbType.VarChar).Value = TextBox1.Text;
com.Parameters.AddWithValue("@Image", SqlDbType.Image).Value = imgarray;
com.Parameters.AddWithValue("@user_id", PID);
com.ExecuteNonQuery();
Label1.Visible = true;
Label1.Text = "Image Is Uploaded successfully";
imagebindGrid();
}
}
}
/* Gridview για εικονες */
public void imagebindGrid()
{
connection();
query = "Select id,ImageName,Image from ImageToDB where user_id= " + PID;
SqlCommand com = new SqlCommand(query, con);
SqlDataReader dr = com.ExecuteReader();
//Image.ImageUrl = "/Handler.ashx?id_Image=" + PID.ToString();
//Gridview1.DataSource = dr;
//Gridview1.DataBind();
}
/*Logout Button */
protected void Button1_Click(object sender, EventArgs e)
{
System.Web.Security.FormsAuthentication.SignOut();
Session.Clear();
Session.RemoveAll();
Session.Abandon();
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate");
HttpContext.Current.Response.AddHeader("Pragma", "no-cache");
HttpContext.Current.Response.AddHeader("Expires", "0");
FormsAuthentication.SignOut();
HttpContext.Current.User = new GenericPrincipal(new GenericIdentity(string.Empty), null);
Response.Redirect("~/Logout.aspx");
}
/* Κλασση για το Post */
private void Txt()
{
try
{
PID = (int)(Session["id"]);
if (PID != null)
{
connection();
query1 = "Insert into Posttext (user_id,Posttext) values (@user_id,@Your_Post)";
SqlCommand com2 = new SqlCommand(query1, con);
com2.Parameters.AddWithValue("@Your_Post", SqlDbType.VarChar).Value = PostBox.Text;
com2.Parameters.AddWithValue("@user_id", PID);
com2.ExecuteNonQuery();
lbluser.Text = PostBox.Text;
PostSelection();
}
}
catch (Exception ex)
{
//con.Close();
}
}
/* Κανει select τα κειμενα και τα ανεβαζει απο την βαση στο grid */
public void PostSelection()
{
connection();
query2 = "Select Posttext from Posttext where user_id= " + PID;
SqlCommand com1 = new SqlCommand(query2, con);
SqlDataReader Read = com1.ExecuteReader();
grdemployee7.DataSource = Read;
grdemployee7.DataBind();
Read.Close();
}
/* Καλειται η Txt() για να γινει το Post */
protected void Button2_Click(object sender, EventArgs e)
{
Txt();
}
public string USER_PID { get; set; }
public DateTime _myid { get; set; }
/* --------------------Κουμπι για search PROFILE -----------------------------------*/
public void Button3_Click1(object sender, EventArgs e)
{
Response.Redirect("~/WebForm7.aspx");
}
public string SN { get; set; }
public string PS { get; set; }
public string EM { get; set; }
public int PID { get; set; }
protected void PostBox_TextChanged(object sender, EventArgs e)
{
}
}
}
这是我的处理程序
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace DisplayingImages
{
/// <summary>
/// Summary description for Handler1
/// this application is created by vithal wadje for C# corner
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Handler1 : IHttpHandler
{
//createting the object of Default.aspx class page to
//call connection and use strings variable
Default cls = new Default();
public void ProcessRequest(HttpContext context)
{
//storing the querystring value that comes from Defaul.aspx page
string displayimgid = context.Request.QueryString["id_Image"].ToString();
cls.connection();
//retriving the images on the basis of id of uploaded
//images,by using the querysting valaues which comes from Defaut.aspx page
cls.query = "select Image from ImageToDB where id=" + displayimgid;
SqlCommand com = new SqlCommand(cls.query, cls.con);
SqlDataReader dr = com.ExecuteReader();
dr.Read();
context.Response.BinaryWrite((Byte[])dr[0]);
context.Response.End();
}
public bool IsReusable
{
get
{
return true;
}
}
}
}
这是我尝试传递id的方式
<asp:Image ID="Image" runat="server" ImageUrl='<%# "Handler1.ashx?id_Image="+ Eval("id") %>' "/>
代码可能看起来有些混乱,但我尝试了一些没有任何成功的事情。
再次感谢!!
答案 0 :(得分:0)
您需要为此设置contenttype
。否则它只是二进制的。使用它。
context.Response.ContentType = "image/png";
这取决于您正在编写的图像类型。所以基于使用image/jpg
等
除此之外,我可以在您的代码中看到,您已经使用imagebindGrid
通过查询到SQL来将图像绑定到网格,但是在绑定到网格之前没有使用dr.Read()
,这不会打开行集,因此没有数据绑定,你没有看到新的图像。所以新代码将如下所示。
SqlDataReader dr = com.ExecuteReader();
dr.Read();
Gridview1.DataSource = dr;
Gridview1.DataBind();
如果您可以在aspx
处显示Gridview1
代码,那么在您更改此内容后遇到问题时,这将对您提供帮助更有帮助。