问题描述
这是完全不同的问题.请有人帮助我.将存储在sql数据库中的图像检索到页面或图像控件中.我从最近四天开始尝试了所有方法..请帮助.
我是ogogesh,我不是asp.net或c#的新手.我已使用以下代码将图像成功存储在sql表中(数据类型->图像)
Its really different problem.please someone help me. in retrieving image stored in sql databaseto to a page or to image control. i tried everything since last four days.. pls help.
HI iam yogesh, i am not new to asp.net or c#. i have succesfully stored images in sql table(datatype-> image), using the following code
HttpPostedFile file = myfile.PostedFile;
int size = file.ContentLength;
string strtype = file.ContentType;
string fn = file.FileName;
byte[] bit = new byte[size];
file.InputStream.Read(bit, 0, bit.Length);
SqlCommand savecmd = new SqlCommand("insert into reginfo (userid,password,name,surname,sex,country,imgdata,imgsize,imgstrtype,imgfilename) values (" + "'" + txtregid.Text + "'," + "'" + txtregpassword.Text + "'," + "'" + txtname.Text + "'," + "'" + txtsurname.Text + "'," + "'" + ddsex.Text + "'," + "'" + ddcountry.Text + "'"+","+"'"+bit+"'"+","+size+","+"'"+strtype+"'"+","+"'"+fn+"'"+")", methods.connection);
savecmd.ExecuteNonQuery();
我在MYPAGE.ASPX中使用了以下内容----
I USED THE FOLLOWING IN MYPAGE.ASPX----
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
现在,当我试图从html图像控件上的表中获取图像时,它显示了没有图像的图像控件.它还在左上角显示了一个小图像图标.
我也使用过image.ashx(处理程序),但仍然失败.我的有处理程序和无处理程序的代码是:
与处理程序--------
now when iam trying to fetch the image from table on html image control it shows the image control without image. it also shows a small image icon on upper left corner.
i have also used image.ashx (handler) but still failed. my code with handler and without handler are:
WITH HANDLER--------
using System;
using System.Data;
using System.Web;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
using salefunctions;
namespace ADVANCED_FORUM
{
/// <summary>
/// Summary description for $codebehindclassname$
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class iamge : IHttpHandler
{
public string quid;
public void ProcessRequest(HttpContext context)
{
if (context.Request.QueryString["user"]!=null)
{
quid = context.Request.QueryString["user"];
context.Response.ContentType="image/jpeg";
Stream strm = showimage(quid);
byte[] buffer = new byte[4096];
int buffseq = strm.Read(buffer,0, 4096);
while(buffseq>0)
{
context.Response.OutputStream.Write(buffer, 0, buffseq);
buffseq = strm.Read(buffer,0, 4096);
}
}
//filemethod();
}
public Stream showimage(string id)
{
methods.createcon("forum"); /* this is method to create connection and forum is database name */
SqlCommand dispimgcmd = new SqlCommand("select imgdata from reginfo where userid=@aid", methods.connection);
dispimgcmd.Parameters.AddWithValue("@aid", id);
object mimg = dispimgcmd.ExecuteScalar();
return new MemoryStream((byte[])mimg);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
无需处理程序的代码(可在页面上显示图像);
CODE WITHOUT HANDLER (TO DISPLAY IMAGE ON PAGE);
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using salefunctions;
namespace ADVANCED_FORUM
{
public partial class imageview : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//filemethod();
methods.createcon("forum"); /* this is method to create connection and forum is database name */
SqlCommand dispimgcmd=new SqlCommand("select * from reginfo where userid=" + "'" + Session["mlinkquid"] + "'", methods.connection);
SqlDataReader drdispimg = dispimgcmd.ExecuteReader();
drdispimg.Read();
Response.Buffer = true;
Response.Clear();
Response.ContentType = (string)drdispimg["imgstrtype"];
Response.OutputStream.Write((byte[])drdispimg["imgdata"],0,(int)drdispimg["imgsize"]);
methods.connection.Close();
Response.End();
//string user = "";
//image1.Src = "iamge.ashx?user=" + Session["mlinkquid"];
}
}
}
plssssssssssssss有人帮帮我...我检查了所有内容,contenttype,contentlength.all都可以,但是我没有得到图像.
我认为iamges有一些问题.因为即使我拖动了img控件并将"src"属性设置为
plsssssssssssss someone help me... i have checked everything,contenttype,contentlength.all is ok but iam not getting the image.
i think there is some problem with iamges. because even when i dragged an img control and set "src" property as
src="d:\images\srk.jpg"
或
src="~/images/srk.jpg"
它在设计时显示图像,但在运行时它显示相同的图像,即当我运行项目时,发生相同的问题,img控件显示无图像.
我厌倦了寻求解决方案.请帮助我.对我来说非常重要...如果ASP中有任何大师,C#
[edit] SHOUTING已删除,主题-OriginalGriff [/edit]
it shows the image atdesign time but it dosent shows the same (at runtime) i:e when i run the project, the same problem happens, img control displays with no image.
plsssssssssssssss iam tired of asking for solution. pls help me. its very imp for me... IF THERE ARE ANY MASTERS IN ASP,C#
[edit]SHOUTING removed, Subject - OriginalGriff[/edit]
推荐答案
无需处理程序的代码(可在页面上显示图像);
CODE WITHOUT HANDLER (TO DISPLAY IMAGE ON PAGE);
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using salefunctions;
namespace ADVANCED_FORUM
{
public partial class imageview : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//filemethod();
methods.createcon("forum"); /* this is method to create connection and forum is database name */
SqlCommand dispimgcmd=new SqlCommand("select * from reginfo where userid=" + "'" + Session["mlinkquid"] + "'", methods.connection);
SqlDataReader drdispimg = dispimgcmd.ExecuteReader();
drdispimg.Read();
Response.Buffer = true;
Response.Clear();
Response.ContentType = (string)drdispimg["imgstrtype"];
Response.OutputStream.Write((byte[])drdispimg["imgdata"],0,(int)drdispimg["imgsize"]);
methods.connection.Close();
Response.End();
//string user = "";
//image1.Src = "iamge.ashx?user=" + Session["mlinkquid"];
}
}
}
plssssssssssssss有人帮帮我...我检查了所有内容,contenttype,contentlength.all都可以,但是我没有得到图像.
我认为iamges有一些问题.因为即使我拖动了img控件并将"src"属性设置为
plsssssssssssss someone help me... i have checked everything,contenttype,contentlength.all is ok but iam not getting the image.
i think there is some problem with iamges. because even when i dragged an img control and set "src" property as
src="d:\images\srk.jpg"
或
src="~/images/srk.jpg"
它在设计时显示图像,但在运行时它显示相同的图像,即当我运行项目时,发生相同的问题,img控件显示无图像.
我厌倦了寻求解决方案.请帮助我.对我来说非常重要...如果ASP中有任何大师,C#
[edit] SHOUTING已删除,主题-OriginalGriff [/edit]
it shows the image atdesign time but it dosent shows the same (at runtime) i:e when i run the project, the same problem happens, img control displays with no image.
plsssssssssssssss iam tired of asking for solution. pls help me. its very imp for me... IF THERE ARE ANY MASTERS IN ASP,C#
[edit]SHOUTING removed, Subject - OriginalGriff[/edit]
<img alt="" src="ImageFromDb.ashx" />
然后,您需要编写代码以访问和显示图像.
在VS中,右键单击您的项目,然后选择添加新项"
出现对话框时,从列表中选择通用处理程序",并将其命名为"ImageFromDb"
按[ENTER].
以下代码将动态显示图像:
You then need to code to access and display the image.
In VS, right click your project, and select "Add new Item"
When the dialog appears, select "Generic Handler" from the list, and call it "ImageFromDb"
Press [ENTER].
The code below will display an image dynamically:
<%@ WebHandler Language="C#" Class="ImageFromDb" %>
using System;
using System.Web;
using System.Data.SqlClient;
public class ImageFromDb : IHttpHandler
{
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
using (SqlConnection con = new SqlConnection(@"Data Source=GRIFFPC\SQLEXPRESS;Initial Catalog=myDatabase;Integrated Security=True"))
{
con.Open();
SqlCommand com = new SqlCommand("SELECT * FROM myTable WHERE id=1", con);
SqlDataReader read = com.ExecuteReader();
if (read.Read())
{
object o = read["Image"];
if (!(o is DBNull))
{
// Image found.
context.Response.ContentType = "image/JPEG";
context.Response.BinaryWrite((byte[]) o);
}
}
}
}
}
您将不得不修改此代码以适合您的数据库和用户ID系统,但这是我使用的完整代码.
感谢您的答复,但它也无效.存在相同的问题."
然后问题出在数据库中的图像数据中,因为该代码是从我的测试站点获取的-运行中.
检查您的插入代码-我只是看了一下,这是非常非常糟糕的做法:SQL注入攻击天壤之别,而且不太可能起作用.
对所有数据库字段使用SqlCommand.Parameters.AddWithValue用参数化的INSERT替换代码,您将得到Bobby Tables的证明,并将正确的数据插入到数据库中.
You will have to modify this code to suit your database and user id system, but that is the complete code I use.
"thanks for reply but it also does''t work. the same problem exists."
Then the problem is in the image data in your database, as that code is taken - working - from my test site.
Check your insert code - I just looked at it and it is very, very bad practice: SQL Injection Attack heaven as well as unlikely to work.
Replace the code with a Parametrized INSERT using SqlCommand.Parameters.AddWithValue for all database fields and you will be both Bobby Tables proof, and inserting the right data to your DB...
这篇关于检索存储在sql数据库中的图像到页面或图像控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!