
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(
)</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 Button
_Click(object sender
System
EventArgs e)

{

int ncount = GetRecordCount();

if (ncount ==
)

{

KunslyWebPage
ShowMessage(this
請先創建相冊
才允許上傳圖片
);

return;

}

// Response
Write(
<script language=javascript>alert(
請先創建相冊
才允許上傳圖片
);history
back(
)</script>
);

//else

//{

if(UploadFile
PostedFile
FileName
Trim() ==
)

{

//Response
Write(
<script language=javascript>alert(
老大
沒文件怎麼傳啊
);history
back(
)</script>
);

KunslyWebPage
ShowMessage(this
請選擇文件
);

return;

}

// else

// {

if (txtMessage
Text
Trim() ==
)

{

KunslyWebPage
ShowMessage(this
請輸入簡介
);

return;

}

//Response
Write(
<script language=javascript>alert(
老大
請輸入簡介
);history
back(
)</script>
);

extension = Path
GetExtension(UploadFile
PostedFile
FileName)
ToUpper();

if (extension !=
JPG
&& extension !=
GIF
)

{

KunslyWebPage
ShowMessage(this
非法文件
不允許上傳
只允許上傳
jpg或
gif文件
);

return;

}

//Response
Write(
<script language=javascript>alert(
非法文件
不允許上傳
);history
back(
)</script>
);

if (UploadFile
PostedFile
ContentLength >=
) //大於
M不允許上傳

{

KunslyWebPage
ShowMessage(this
文件過大
不能上傳
單個文件最大不超過
M
);

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=
newheight;

System
Drawing
Image image
;

image
= System
Drawing
Image
FromFile(path);

width=image
Width;

height=image
Height;

newheight=height*newwidth/width;

Bitmap bm = new Bitmap(image
newwidth
newheight);

bm
Save(Server
MapPath(
) +
/upfile/
+ fileName +
_s
+ extension
ImageFormat
Jpeg);

image
= System
Drawing
Image
FromFile(path);

LblShowPhoto
Text =
照片已經成功上傳
文件名為
+ fileName + extension +
占用空間
;

LblShowPhoto
Text += UploadFile
PostedFile
ContentLength/
+
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
PhotoColl
)
;

strSql +=
values
;

strSql +=
(
+fileName+extension+
+fileName +
_s
+ extension+
+image
Width+
+image
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();

image
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 =
;

if(dbReader
Read())

nCount = Convert
ToInt
(dbReader[
tt
]);

dbReader
Close();

dbConn
Close();

return nCount;

}


#endregion




}

}
From:http://tw.wingwit.com/Article/program/net/201311/12300.html