获取上传的图片byte
protected byte[] getimage()
{//op 是fileupload
if (op.HasFile)
{
Stream fstream = op.PostedFile.InputStream;
HttpPostedFile file = op.PostedFile;
int fileleth = op.PostedFile.ContentLength;
byte[] image = new byte[fileleth];
fstream.Read(image, 0, fileleth);
return image;
}
else return null;
}
____________________________________________________________
将其插入到数据库
public static bool insterm( string name,byte[]f)
{
SqlConnection scc = new SqlConnection();
scc.ConnectionString = "Data Source=cokiwin;Initial Catalog=wyg;Integrated Security=True";
scc.Open();
SqlCommand scmd = new SqlCommand();
scmd.Connection = scc;
scmd.CommandText = "update users set image='"+f+"'where name='" + name + "'";
int y = scmd.ExecuteNonQuery();
if (y > 0)
{
scc.Close();
return true;
}
else { return false; }
}
_________________________________________________________________
获取在数据库中image byte并打印出来
public static byte[] getimage(string name)
{
SqlConnection sc = new SqlConnection();
sc.ConnectionString = "Data Source=cokiwin;Initial Catalog=wyg;Integrated Security=True";
sc.Open();
SqlCommand scm = new SqlCommand();
scm.Connection = sc;
scm.CommandText = "select image from users where name='" + name + "'";
byte[] imagebyte = (byte[])scm.ExecuteScalar();
if (imagebyte.Length > 0)
{
return imagebyte;
}
else { return null; }
}
protected void Button1_Click(object sender, EventArgs e)
{
if (getimage(TextBox1.Text )!= null)
{
Response.ContentType = "image/GIF";
Response.BinaryWrite(getimage(TextBox1.Text));
}
_________________________________________________________________________调用后没包粗也没东西出来!请各位看看有什么问题!~
protected byte[] getimage()
{//op 是fileupload
if (op.HasFile)
{
Stream fstream = op.PostedFile.InputStream;
HttpPostedFile file = op.PostedFile;
int fileleth = op.PostedFile.ContentLength;
byte[] image = new byte[fileleth];
fstream.Read(image, 0, fileleth);
return image;
}
else return null;
}
____________________________________________________________
将其插入到数据库
public static bool insterm( string name,byte[]f)
{
SqlConnection scc = new SqlConnection();
scc.ConnectionString = "Data Source=cokiwin;Initial Catalog=wyg;Integrated Security=True";
scc.Open();
SqlCommand scmd = new SqlCommand();
scmd.Connection = scc;
scmd.CommandText = "update users set image='"+f+"'where name='" + name + "'";
int y = scmd.ExecuteNonQuery();
if (y > 0)
{
scc.Close();
return true;
}
else { return false; }
}
_________________________________________________________________
获取在数据库中image byte并打印出来
public static byte[] getimage(string name)
{
SqlConnection sc = new SqlConnection();
sc.ConnectionString = "Data Source=cokiwin;Initial Catalog=wyg;Integrated Security=True";
sc.Open();
SqlCommand scm = new SqlCommand();
scm.Connection = sc;
scm.CommandText = "select image from users where name='" + name + "'";
byte[] imagebyte = (byte[])scm.ExecuteScalar();
if (imagebyte.Length > 0)
{
return imagebyte;
}
else { return null; }
}
protected void Button1_Click(object sender, EventArgs e)
{
if (getimage(TextBox1.Text )!= null)
{
Response.ContentType = "image/GIF";
Response.BinaryWrite(getimage(TextBox1.Text));
}
_________________________________________________________________________调用后没包粗也没东西出来!请各位看看有什么问题!~
字符串肯定不行,用参数
/// 上传图片并显示出来
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void upload_Click(object sender, ImageClickEventArgs e)
{
string test = Server.MapPath("SkinImage"); //用来生成文件夹
if (!Directory.Exists(test))
{
Directory.CreateDirectory(test);
}
if (FileUpload1.PostedFile.FileName != "")
{
string imgname = FileUpload1.PostedFile.FileName;
string imgType = imgname.Substring(imgname.LastIndexOf(".") + 1);
string quanname = templetName.Value+ "." + imgType;
if (imgType.ToLower() != "gif" && imgType.ToLower() != "jpg" && imgType.ToLower() != "bmp"&& imgType.ToLower()!="jpeg")
{ Response.Write("<script>alert('请选择gif,jpg,bmp,jpeg格式的文件!');</script>"); }
string imgurl = "../AllAdmin/SkinImage/" + quanname;
FileUpload1.PostedFile.SaveAs(Server.MapPath(imgurl));
this.Image1.ImageUrl = Server.UrlDecode(imgurl); this.HFurl.Value = imgurl;
}
else
{Response.Write("<script>alert('请选择文件上传!');</script>");}
}
我的这个是笨法子。你参考下吧。
scmd.CommandText = "update users set image=@f where name=@name";
scmd.Parameters.AddWithValue("@f",f);
scmd.Parameters.AddWithValue("@name", name);
如果我要在grideview里显示多行信息(包括图片)的话是不是先要有个 show图片的
aspx与grideview中的image绑定url? 如果多行显示的话的 参数要怎么传递?
<IMG width=100 height=100 alt="" src='<%# "ReadImage.aspx?ImageID="+DataBinder.Eval(Container,"DataItem.ID")%>'">
http://blog.csdn.net/sunnystar365/archive/2005/10/10/498719.aspx