如何上传图片和现实
学习 2009-12-31 19:23:37 阅读17 评论2 字号:大中小 订阅
以下为操作代码:
using System.IO;
using System.Data.SqlClient;
#region 图片操作
private void pic_upload(string id)//上传操作
{
bool fileOK = false;
string path = Server.MapPath(“~/upload/”);//将文件存到服务器目录upload下
if (FileUpload1.HasFile)
{
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
//string[] allowedExtensions = { “.gif”, “.png”, “.bmp”, “.jpg” };
string[] allowedExtensions = { “.jpg” };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
if (fileOK)
{
DBClient db = new DBClient();
SqlConnection con = db.createConnection();
try
{
if (int.Parse(this.FileUpload1.PostedFile.ContentLength.ToString()) / 1024 > 50)
{
Response.Write(“<script defer>alert(\’图片太大,超过50K,不能上传!请用相应图片处理软件将图片保存为宽112,高116的JPG格式图片。\’);</script>”);
}
else
{
//FileUpload1.SaveAs(path + id + “.jpg”);//如果想保存图片,请不要注释这句
//this.Image1.ImageUrl = “~/upload/” + id + “.jpg”;
//图片上传,转为字节流写到数据库中
Stream imgStream = FileUpload1.PostedFile.InputStream;
int imgLen = FileUpload1.PostedFile.ContentLength;
byte[] imgdata = new byte[imgLen];
int n = imgStream.Read(imgdata, 0, imgLen);
string sql = “UPDATE [gyl_huliny] SET [zhaopian]=@zhaopian WHERE huliny_id=\'” + id + “\'”;
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = sql;
cmd.Parameters.Add(“@zhaopian”, imgdata);
con.Open();
if (cmd.ExecuteNonQuery() > 0)
{
this.pic_load(id);
Response.Write(“<script>window.location=window.location;</script>”);
}
}
}
catch (Exception ex)
{
Response.Write(“<script defer>alert(\’文件上传失败!” + ex.Message + “\’);</script>”);
}
finally
{
con.Close();
}
}
else
{
Response.Write(“<script defer>alert(\’只能上传.jpg格式图片\’);</script>”);
}
}
}
private void pic_load(string id)//加载图片
{
this.Image1.ImageUrl = “org_cun_hly_edit_pic_read.aspx?hly_id=”+id;
}
protected void btn_upload_Click(object sender, EventArgs e)//点了上传
{
this.pic_upload(Request.Params[“hly_id”].ToString().Trim());
}
#endregion
浪费一张页面来加载图片,设计里不要拖上任何控件,看代码:
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 org_org_cun_hly_edit_pic_read : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Request.Params[“hly_id”] != null)
{
this.hly_edit_pic_read(Request.Params[“hly_id”].ToString().Trim());
}
}
}
#region 从数据库中读取图片
private void hly_edit_pic_read(string id)
{
DBClient db = new DBClient();
string sql = ” select [zhaopian] from gyl_huliny WHERE zhaopian is not null and huliny_id=\'” + id + “\'”;
SqlConnection con = db.createConnection();
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = sql;
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (dr != null)
{
while (dr.Read())
{
Response.BinaryWrite((byte[])dr[“zhaopian”]);//直接写二进制流到页面
}
}
}
}
#endregion
}
本文来源于Woody的鸟窝(Woody\’s Blog) http://www.smartgz.com/, 原文地址:http://www.smartgz.com/blog/Article/837.asp