图片上传代码示例
using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using KunslyNetDll.Common;
using HunMSM.include;
namespace HunMSM.photo
...{
/**//// <summary>
/// 图片上传。
/// </summary>
public partial class _upphoto : System.Web.UI.Page
...{
public string sPath = "";
public string extension;
public string path;
public string fileName;
public string smobile;
protected void Page_Load(object sender, System.EventArgs e)
...{
// 在此处放置用户代码以初始化页面
AddControl();
if (Session["UserMobile"] == null)
Response.Write("<script language=javascript>alert('请先登录,才允许上传图片');history.back(-1)</script>");
else
smobile = Session["UserMobile"].ToString().Trim();
if(!this.IsPostBack)
...{
DropDownPhotoType();
DropDownPhotoColl();
}
}
void AddControl()#region void AddControl()
/**//// <summary>
/// 添加控件
/// </summary>
private void AddControl()
...{
Control ctrl;
sPath = KunslyWebPage.GetRelativeRootPath(this);
//头
ctrl = LoadControl(sPath + "CtrlHead.ascx");
HolderHead.Controls.Add(ctrl);
//左边框
ctrl = LoadControl(sPath + "CtrlLeft.ascx");
HolderLeft.Controls.Add(ctrl);
//底部
ctrl = LoadControl(sPath + "CtrlBottom.ascx");
HolderBottom.Controls.Add(ctrl);
}
#endregion
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
}
#endregion
protected void Button1_Click(object sender, System.EventArgs e)
...{
int ncount = GetRecordCount();
if (ncount == 0)
...{
KunslyWebPage.ShowMessage(this,"请先创建相册,才允许上传图片");
return;
}
// Response.Write("<script language=javascript>alert('请先创建相册,才允许上传图片');history.back(-1)</script>");
//else
//{
if(UploadFile.PostedFile.FileName.Trim() == "")
...{
//Response.Write("<script language=javascript>alert('老大,没文件怎么传啊');history.back(-1)</script>");
KunslyWebPage.ShowMessage(this,"请选择文件");
return;
}
// else
// {
if (txtMessage.Text.Trim() == "")
...{
KunslyWebPage.ShowMessage(this,"请输入简介");
return;
}
//Response.Write("<script language=javascript>alert('老大,请输入简介');history.back(-1)</script>");
extension = Path.GetExtension(UploadFile.PostedFile.FileName).ToUpper();
if (extension != ".JPG" && extension != ".GIF")
...{
KunslyWebPage.ShowMessage(this,"非法文件,不允许上传,只允许上传.jpg?http://www.xvna.com或.gif?http://www.xvna.com文件");
return;
}
//Response.Write("<script language=javascript>alert('非法文件,不允许上传');history.back(-1)</script>");
if (UploadFile.PostedFile.ContentLength >= 1000000) //大于1M不允许上传
...{
KunslyWebPage.ShowMessage(this,"文件过大,不能上传,单个文件最大不超过1M");
return;
}
fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
path = Server.MapPath("") + "/upfile/" + fileName + extension;
updata();
}
void updata()#region void updata()
void updata()
...{
//这个过程可以更新的数据库,把图片地址插入到数据库
UploadFile.PostedFile.SaveAs(path);
int width,height,newwidth=100,newheight;
System.Drawing.Image image1;
image1 = System.Drawing.Image.FromFile(path);
width=image1.Width;
height=image1.Height;
newheight=height*newwidth/width;
Bitmap bm = new Bitmap(image1,newwidth, newheight);
bm.Save(Server.MapPath("") + "/upfile/" + fileName + "_s" + extension, ImageFormat.Jpeg);
image1 = System.Drawing.Image.FromFile(path);
LblShowPhoto.Text = "照片已经成功上传,文件名为:" + fileName + extension + " 占用空间:";
LblShowPhoto.Text += UploadFile.PostedFile.ContentLength/1024 + "k";
string strtxtMessage = txtMessage.Text;
strtxtMessage = strtxtMessage.Replace("'","'");
strtxtMessage = strtxtMessage.Replace(""",""");
strtxtMessage = strtxtMessage.Replace(" ","<br>");
string strSql = "insert into photourl ";
strSql += "(PhotoUrl,PhotoUrl_s,PhotoWidth,PhotoHeight,PhotoSize,Puttime,PhotoIntro,";
strSql += "PhotoColl,Mobile,PhotoColl1)";
strSql += " values ";
strSql += "('"+fileName+extension+"','"+fileName + "_s" + extension+"',"+image1.Width+","+image1.Height;
strSql += ","+UploadFile.PostedFile.ContentLength+",getdate(),'"+strtxtMessage;
strSql += "',"+DdlPhotoType.SelectedValue.ToString().Trim()+",'"+smobile+"',";
strSql += DdlPhotoColl.SelectedValue.ToString().Trim()+")";
SqlConnection dbConn = DbConnection.GetSqlConn();
SqlCommand dbCmd = new SqlCommand(strSql, dbConn);
dbCmd.ExecuteNonQuery();
dbCmd.Dispose();
dbConn.Close();
image1.Dispose();
bm.Dispose();
}
#endregion
void DropDownPhotoType()#region void DropDownPhotoType()
void DropDownPhotoType()
...{
//这个过程是将照片类型绑定
string strSql = "select * from photocolltype";
SqlConnection dbConn = DbConnection.GetSqlConn();
SqlCommand dbCmd = new SqlCommand(strSql, dbConn);
SqlDataReader dbReader = dbCmd.ExecuteReader();
DdlPhotoType.Items.Clear();
while (dbReader.Read())
...{
DdlPhotoType.Items.Add(new ListItem(dbReader["CollType"].ToString(),dbReader["PhotoID"].ToString()));
}
dbReader.Close();
dbConn.Close();
}
#endregion
void DropDownPhotoColl()#region void DropDownPhotoColl()
void DropDownPhotoColl()
...{
//这个过程是将相册类型绑定
string strSql = "select * from PhotoColl where mobile=" +"'"+smobile+"'";
SqlConnection dbConn = DbConnection.GetSqlConn();
SqlCommand dbCmd = new SqlCommand(strSql, dbConn);
SqlDataReader dbReader = dbCmd.ExecuteReader();
DdlPhotoColl.Items.Clear();
while (dbReader.Read())
...{
DdlPhotoColl.Items.Add(new ListItem(dbReader["PhotoColl"].ToString(),dbReader["PhotoID"].ToString()));
}
dbReader.Close();
dbConn.Close();
}
#endregion
int GetRecordCount()#region int GetRecordCount()
/**//// <summary>
/// 取得记录数
/// </summary>
/// <returns></returns>
private int GetRecordCount()
...{
string strSql = "select count(*) as tt";
strSql += " from photocoll";
strSql += " where mobile=" + smobile;
SqlConnection dbConn = DbConnection.GetSqlConn();
SqlCommand dbCmd = new SqlCommand(strSql, dbConn);
SqlDataReader dbReader = dbCmd.ExecuteReader();
int nCount = 0;
if(dbReader.Read())
nCount = Convert.ToInt32(dbReader["tt"]);
dbReader.Close();
dbConn.Close();
return nCount;
}
#endregion
}
}