user_board_imageupload.aspx.cs
资源名称:论坛源码.rar [点击查看]
上传用户:scene123
上传日期:2010-02-19
资源大小:3311k
文件大小:5k
源码类别:
.net编程
开发平台:
C#
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Web;
- using System.Web.SessionState;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.HtmlControls;
- using System.Data.OleDb;
- using System.Configuration;
- using System.Text;
- using System.IO;
- namespace kuangren
- {
- /// <summary>
- /// user_board_imageupload 的摘要说明。
- /// </summary>
- public class user_board_imageupload : System.Web.UI.Page
- {
- protected System.Web.UI.HtmlControls.HtmlInputButton Button1;
- protected System.Web.UI.HtmlControls.HtmlInputFile myFile;
- protected System.Web.UI.WebControls.Label Label1;
- public string mmstr1;
- public string strFileExt;
- public int filelength;
- protected int intRunTime;
- protected DateTime dtStartTime = DateTime.Now;
- protected System.Web.UI.HtmlControls.HtmlInputButton Upload;
- public string usercookies;
- public string newname;
- protected System.Data.OleDb.OleDbCommand cmd;
- protected System.Data.OleDb.OleDbConnection conn;
- public string passhow;
- public DataSet dsPending;
- public string sql;
- public string passshow;
- public string OleDbsql;
- private void Page_Load(object sender, System.EventArgs e)
- {
- if (Request.Cookies["jusername"]!=null)
- {
- usercookies=Request.Cookies["jusername"].Value;
- }
- else
- {
- Response.Redirect("logine.aspx");
- }
- conn = new OleDbConnection(ConfigurationSettings.AppSettings["cnFriends.ConnectionString"]);
- sql="SELECT u_images from userinfo where u_user='"+usercookies+"'";
- dsPending=new DataSet();
- OleDbDataAdapter adWord=new OleDbDataAdapter(sql,conn);
- adWord.Fill(dsPending,"userinfo");
- Page.DataBind();
- passshow=Convert.ToString(dsPending.Tables[0].Rows[0]["u_images"]);
- }
- #region Web 窗体设计器生成的代码
- override protected void OnInit(EventArgs e)
- {
- //
- // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
- //
- InitializeComponent();
- base.OnInit(e);
- }
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.Upload.ServerClick += new System.EventHandler(this.Upload_ServerClick);
- this.Load += new System.EventHandler(this.Page_Load);
- }
- #endregion
- private void Upload_ServerClick(object sender, System.EventArgs e)
- {
- if(myFile.PostedFile!=null)
- {
- //定义变量
- string strFileName = myFile.PostedFile.FileName;
- strFileExt = System.IO.Path.GetExtension(strFileName).ToString();//
- filelength=Convert.ToInt32(myFile.PostedFile.ContentLength.ToString());
- string path = Server.MapPath("./webdesign/userupload");
- string nam = myFile.PostedFile.FileName ;
- int i= nam.LastIndexOf("\") ;
- //取得当前日期作为文件名
- DateTime now =DateTime.Now;
- newname=now.ToString();
- newname=newname.Replace(" ","");
- newname=newname.Replace("-","");
- newname=newname.Replace(":","");
- Random rnd = new Random();
- double rndNum=rnd.NextDouble()*10000000000000;
- newname=newname+rndNum.ToString().Substring(0,rndNum.ToString().LastIndexOf("."))+strFileExt;
- //获取结束
- string newnm ="\"+newname+"" ;
- path += newnm;
- if(File.Exists(path) == true)
- {
- Label1.Text = "<font color=red>文件名重复,请更改文件名重新上传..</font>";
- return;
- }
- //改变下面的“c:\"到你想要保存的地址。
- if ((strFileExt==".jpg"||strFileExt==".jpeg"||strFileExt==".gif")&&(filelength<1024*15))
- {
- string path1 =Server.MapPath("./webdesign/userupload/"+passshow+"");
- if(File.Exists(path1) == true)
- {
- System.IO.File.Delete(Server.MapPath("./webdesign/userupload/"+passshow+""));
- }
- myFile.PostedFile.SaveAs(path);
- Label1.Text ="<font color=red>文件上传成功, </font><a href='user_board_imageupload.aspx'><font color=red>请返回..</font></a>";
- StringBuilder sb=new StringBuilder();
- ArrayList values=new ArrayList();
- sb.Append("update [userinfo] set u_images='"+newname+"' where u_user='"+usercookies+"'");
- OleDbsql=String.Format(sb.ToString(),values.ToArray());
- cmd=new OleDbCommand(OleDbsql,conn);
- conn.Open();
- bool doredirect=true;
- try
- {
- cmd.ExecuteNonQuery();
- }
- catch
- {
- doredirect=false;
- }
- finally
- {
- conn.Close();
- }
- if(doredirect)
- {
- }
- else
- {
- Response.Redirect("index.asp");
- }
- }
- else
- {Label1.Text ="<font color=red>1、上传支持格式:GIF,JPG,JPEG。<br>2、文件不能大于15K。<a href='user_board_imageupload.aspx'><font color=red>重新上传..</font></a></font>";}
- }
- }
- }
- }