imagePage.aspx代码如下:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="imagePage.aspx.cs" Inherits="imagePage" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal">
<ItemTemplate>
<table style="width: 80px; height: 100px" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:ImageButton ID="ImageButton1" runat="server" Height="100px" Width="78px" ImageUrl='<%#DataBinder.Eval(Container.DataItem,"ImageData") %>'/></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
imagePage.aspx.cs代码如下: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.Data.SqlClient;public partial class imagePage : System.Web.UI.Page
{
SqlConnection sqlcon;
string strCon = "Data Source=(local);Database=EX_NEW;Uid=sa;Pwd=photosystem";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind(DataList1);
}
}
public void bind(DataList dl)
{
PagedDataSource ps = new PagedDataSource();
string sqlstr = "select ImageID,ImageData from ImageStore order by ImageID";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "ImageStore");
ps.DataSource = myds.Tables["ImageStore"].DefaultView;
ps.AllowPaging = true; //是否可以分页
ps.PageSize = 5; //显示的数量
dl.DataSource = ps;
dl.DataKeyField = "ImageID";
dl.DataBind();
sqlcon.Close();
}
}运行后,只看到"红叉"!
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal">
<ItemTemplate>
<table style="width: 80px; height: 100px" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:ImageButton ID="ImageButton1" runat="server" Height="100px" Width="78px" ImageUrl='<%#DataBinder.Eval(Container.DataItem,"ImageData") %>'/></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
imagePage.aspx.cs代码如下: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.Data.SqlClient;public partial class imagePage : System.Web.UI.Page
{
SqlConnection sqlcon;
string strCon = "Data Source=(local);Database=EX_NEW;Uid=sa;Pwd=photosystem";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind(DataList1);
}
}
public void bind(DataList dl)
{
PagedDataSource ps = new PagedDataSource();
string sqlstr = "select ImageID,ImageData from ImageStore order by ImageID";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "ImageStore");
ps.DataSource = myds.Tables["ImageStore"].DefaultView;
ps.AllowPaging = true; //是否可以分页
ps.PageSize = 5; //显示的数量
dl.DataSource = ps;
dl.DataKeyField = "ImageID";
dl.DataBind();
sqlcon.Close();
}
}运行后,只看到"红叉"!
你查查生成页面后的html代码,看看图片的url是啥?然后看看路径是否对
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Linq;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class UploadImage : Page
{
protected HtmlInputFile UP_FILE; //HtmlControl、WebControls控件对象
protected TextBox txtDescription;
protected Label txtMessage;
protected Int32 FileLength = 0; //记录文件长度变量
protected void Button_Submit(System.Object sender, System.EventArgs e)
{
HttpPostedFile UpFile = UP_FILE.PostedFile; //HttpPostedFile对象,用于读取图象文件属性
FileLength = UpFile.ContentLength; //记录文件长度
try
{
if (FileLength == 0)
{ //文件长度为零时
txtMessage.Text = "<b>请你选择你要上传的文件</b>";
}
else
{
Byte[] FileByteArray = new Byte[FileLength]; //图象文件临时储存Byte数组
Stream StreamObject = UpFile.InputStream; //建立数据流对像
//读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
StreamObject.Read(FileByteArray, 0, FileLength);
//建立SQL Server链接
SqlConnection Con = new SqlConnection("Data Source=Localhost; Initial Catalog=EX_NEW; User ID=sa;Pwd=photosystem;");
String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType, ImageDescription, ImageSize) VALUES (@Image, @ContentType, @ImageDescription, @ImageSize)";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@Image", SqlDbType.Binary, FileLength).Value = FileByteArray;
CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = UpFile.ContentType; //记录文件类型
//把其它单表数据记录上传
CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDescription.Text;
//记录文件长度,读取时使用
CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt, 8).Value = UpFile.ContentLength;
Con.Open();
CmdObj.ExecuteNonQuery();
Con.Close();
txtMessage.Text = "<p><b>OK!你已经成功上传你的图片</b>";//提示上传成功
}
}
catch (Exception ex)
{
txtMessage.Text = ex.Message.ToString();
}
}
}
这是小弟的上传模块,上传图片,保存的是二进制数据,所以关于路径,真的无从可知了
{
MemoryStream ms = new MemoryStream(byteArrayIn, 0, count);
Image returnImage = Image.FromStream(ms);
return returnImage;
} using System.IO;
using System.Drawing;不要用byte[],路径很好啊
建立一个PhotoHandler.ashx的处理页面.
public class PhotoHandler : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false; int photoId = -1;
Stream stream = null; if (context.Request.QueryString["PhotoID"] != null && context.Request.QueryString["PhotoID"] != "")
{
photoId = Convert.ToInt32(context.Request.QueryString["PhotoID"]);
MyDB db = new MyDB();
stream = db.GetPhoto(photoId);//自己写的获取数据库的图片数据
}
const int buffierSize = 1024 * 16;
byte[] buffer = new byte[buffierSize];
int count = stream.Read(buffer, 0, buffierSize);
while (count > 0)
{
context.Response.OutputStream.Write(buffer, 0, count);
count = stream.Read(buffer, 0, buffierSize);
}
} public bool IsReusable
{
get
{
return true;
}
}
}
//获取数据库中图片的二进制数据
public Stream GetPhoto(int photoID)
{
String cmdText = "select [Data] from [Image] where ID = @ID";
SqlCommand cmd = new SqlCommand(cmdText, conn);
cmd.Parameters.AddWithValue("@ID", photoID);
conn.Open();
object result = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])result);
}
catch (ArgumentNullException ex)
{
return null;
}
finally
{
conn.Close();
}
}
你查查生成页面后的html代码,看看图片的url是啥?然后看看路径是否对
代码太长,把紧要的贴出来就行了啦,再看一下HTML代码是什么路径吧,按HTML代码你试一下这个路径下能看到图片不,如果看不到,肯定就是路径出错了。
这个应该是ASP.NET吧.但我写的是C#哦.所以还是不懂...
//上传图片的方法
string fileName = Path.GetFileName(upfile.FileName);//获取带
string fielNewName = DateTime.Now.ToString("yyyymmmddhhmmss") +fileName;
string path = Server.MapPath("你的文件夹名称"+fileNewName);
upfile.SaveAs(path);
//数据库操作,将原本的ImageData改成ImageName,类型为string
string sql = "insert into ImageStore (ImageName,...)"
....
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="piclist.aspx.cs" Inherits="Test_Test" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
<style type="text/css">
#img1
{
width: 229px;
height: 209px;
}
.style1
{
height: 26px;
width: 245px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="dlContent" runat="server" Width="242px" style="margin-right: 0px">
<ItemTemplate>
<table cellpadding="0" cellspacing="0">
<tr>
<td style="width: 554px; text-align: left; height: 26px;">
<img id='img1' src='StreamImg.aspx?id=<%# DataBinder.Eval(Container.DataItem,"ImageID") %>' >
</a>
</a>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
piclist.aspx.cs 如下: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.Data.SqlClient;
using System.IO;
public partial class Test_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//连接数据库
string ConnStr = "Data Source=Localhost; Initial Catalog=EX_NEW; User ID=sa;Pwd=photosystem;";
SqlConnection sqlcon = new SqlConnection(ConnStr);
sqlcon.Open();
string sqlstr = "select ImageID from ImageStore";
SqlDataAdapter MyAdapter = new SqlDataAdapter(sqlstr, sqlcon);
DataSet ds = new DataSet();
MyAdapter.Fill(ds, "ImageData");
this.dlContent.DataSource = ds;
this.dlContent.DataBind();
sqlcon.Close();
}
}
}
然后这个是中转页,是用来绑定ImageID的.StreamImg.aspx是没有代码的.StreamImg.aspx.cs 如下:using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;public partial class StreamImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//string type = Request.QueryString["pt"];
int id = Convert.ToInt32(Request.QueryString["ImageID"]);
Showpic(id);
} private void Show(int id)
{
throw new NotImplementedException();
}
private void Showpic(int id)
{
//连接数据库
string ConnStr = "Data Source=Localhost; Initial Catalog=EX_NEW; User ID=sa;Pwd=photosystem;";
string strSql = "select * from ImageStore where ImageID='" + id + "'";
SqlConnection conn = new SqlConnection(ConnStr);
conn.Open(); SqlCommand cmd = new SqlCommand(strSql, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["ImageData"]);
Response.Write("successful"); }
reader.Close();
conn.Close();
Response.End();
}
}
<Image ID="img1" runat="server" ImageUrl='<%# "StreamImg.aspx?id="+Eval("ImageID")%>'>,而且你不必须循环sqldatareader,你只是要取该ID的图象,直接reader["ImageData"]就行了。