新手~!image控件显示本地图片问题
以下为起始页文档Default.aspx;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBox1.Items.Count > 0)
{
TextBox1.Text = ListBox1.SelectedItem.Text;
Image1.ImageUrl = "Show.aspx?uid=" + ListBox1.SelectedValue;
}
}
rotected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<script>alert('成功~!')</script>");
Image1.ImageUrl="image.aspx?uid="+FileUpload1.PostedFile.FileName;//image控件url指定为image.aspx }
protected void Button3_Click(object sender, EventArgs e)
{
if (ListBox1.Items.Count > 0)
{
string con = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
SqlConnection sqlcon = new SqlConnection(con);
string sqlstr = "delete from photolist where id=@id";
SqlCommand cmd = new SqlCommand(sqlstr, sqlcon);
TextBox1.Text = ListBox1.SelectedItem.Text;
cmd.Parameters.Add("@id", SqlDbType.Int).Value = ListBox1.SelectedValue;
sqlcon.Open();
if (cmd.ExecuteNonQuery() > 0)
{
Response.Write("<script>alert('删除成功~!')</script>");
ListBox1.DataBind();
ListBox1.SelectedIndex = ListBox1.Items.Count - 1;
ListBox1_SelectedIndexChanged(sender, e);
}
if (ListBox1.Items.Count > 0)
TextBox1.Text = ListBox1.SelectedItem.Text;
else
TextBox1.Text = null;
}
else {
Response.Write("<script>alert('document is empty~!')</script>");
}
}
protected void Button4_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string con = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
string SelSQL = "Insert photolist Values(@name,@Photo)";
SqlConnection mycon = new SqlConnection(con);
SqlCommand cmd = new SqlCommand(SelSQL, mycon);
cmd.Parameters.Add("@name", SqlDbType.VarChar);
cmd.Parameters["@name"].Value = TextBox1.Text;
#region //构造一个Byte 数组,用以接收上传图片的流数据
byte[] uploadimage = null;
int len = FileUpload1.PostedFile.ContentLength;
uploadimage = new byte[len];
Stream mystream = FileUpload1.PostedFile.InputStream;
mystream.Read(uploadimage, 0, len);
#endregion
cmd.Parameters.Add("@Photo", SqlDbType.VarBinary);
cmd.Parameters["@Photo"].Value = uploadimage;
mycon.Open();
try
{
cmd.ExecuteNonQuery();
Response.Write("<script>alert('图片上传成功!')</script>");
ListBox1.DataBind();
ListBox1.SelectedIndex = ListBox1.Items.Count - 1;
ListBox1_SelectedIndexChanged(sender, e);
}
catch (SqlException ex)
{
throw (ex);
}
}
else
{
Response.Write("<script>alert('请指定加载图片的正确路径~!')</script>");
}
}
}
image.aspx 文档 本地读取并显示图片,vs2008调试的时候这里的图片可以显示,可是发布到IIS7上的时候这里就无法显示图片了,图片显示为"叉叉",路径显示却是对的"http://localhost:49478/Test1106/image.aspx?uid=C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO ;
public partial class image : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str =Request.QueryString["uid"].ToString ();//获取要显示的图片路径
if(str !=null)
{
#region
FileStream fs = new FileStream(str, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
int count = (int)fs.Length;
byte[] b=new byte[count];
b = r.ReadBytes(count);//读取字节数组;
#endregion
if (count> 0)
{
Response.Clear();
Response.ContentType = "image/jpeg";
//Response.WriteFile(str);
Response.BinaryWrite(b);//b为图片内容的字节数组,在该页显示该图片;
Response.End();
}
}
}
}
show.aspx 文档using System从数据库读取并显示图片,无论是在vs2008调试还是IIS7显示的图片都是“正常”的;
using System.Data;
using System.Data.SqlClient;
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;
public partial class Show : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["uid"] == null)
{
Response.Redirect("Default.aspx");
return;
}
Response.Clear();
string con = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
string SelSQL = "SELECT Photo FROM photolist WHERE id=@id";
SqlConnection mycon = new SqlConnection(con);
SqlCommand cmd = new SqlCommand(SelSQL,mycon);
cmd.Parameters.Add("@id", SqlDbType.Int);
cmd.Parameters["@id"].Value = int.Parse(Request.QueryString["uid"]);
mycon.Open();
try
{
SqlDataReader read = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (read.Read())
{
Response.ContentType = "image/gif";
Response.BinaryWrite((byte[])read["Photo"]);
Response.End();
mycon.Close();
}
}
catch (Exception ex)
{
throw(ex);
}
}
}
image.aspx 文档是从本地读取并显示图片,vs2008调试的时候image.aspx这里的图片可以显示,可是发布到IIS7上的时候image.aspx 页的图片就无法显示了,图片显示为"叉叉",路径却是对的"http://localhost:49478/Test1106/image.aspx?uid=C:\Users\Public\Pictures\Sample Pictures\Koala.jpg
问题出在哪???请高手指教本人用的是windows7 IIS7,vs2008调试的