熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

圖片上傳代碼示例

2022-06-13   來源: .NET編程 

using System;
using SystemIO;
using SystemCollections;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemDrawingImaging;
using SystemWeb;
using SystemWebSessionState;
using SystemWebUI;
using SystemWebUIWebControls;
using SystemWebUIHtmlControls;

using SystemDataSqlClient;
using KunslyNetDllCommon;
using HunMSMinclude;

namespace HunMSMphoto
{
    /**//// <summary>
    /// 圖片上傳
    /// </summary>
    public partial class _upphoto : SystemWebUIPage
    {
        public string sPath = ;
        public string extension;
        public string path;
        public string fileName;
        public string smobile;
        protected void Page_Load(object sender SystemEventArgs e)
        {
            // 在此處放置用戶代碼以初始化頁面
            AddControl();
            if (Session[UserMobile] == null)            
                ResponseWrite(<script language=javascript>alert(請先登錄才允許上傳圖片);historyback()</script>);
            else
                smobile = Session[UserMobile]ToString()Trim();
            if(!thisIsPostBack)
            {
                DropDownPhotoType();
                DropDownPhotoColl();
            }
        }

        void AddControl()#region void AddControl()

        /**//// <summary>
        /// 添加控件
        /// </summary>
        private void AddControl()
        {
            Control ctrl;
            
            sPath = KunslyWebPageGetRelativeRootPath(this);

            //頭
            ctrl = LoadControl(sPath + CtrlHeadascx);
            HolderHeadControlsAdd(ctrl);

            //左邊框
            ctrl = LoadControl(sPath + CtrlLeftascx);
            HolderLeftControlsAdd(ctrl);

            //底部
            ctrl = LoadControl(sPath + CtrlBottomascx);
            HolderBottomControlsAdd(ctrl);
        }

        #endregion    

        Web 窗體設計器生成的代碼#region Web 窗體設計器生成的代碼
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 該調用是 ASPNET Web 窗體設計器所必需的
            //
            InitializeComponent();
            baseOnInit(e);
        }
        
        /**//// <summary>
        /// 設計器支持所需的方法  不要使用代碼編輯器修改
        /// 此方法的內容
        /// </summary>
        private void InitializeComponent()
        {    

        }
        #endregion

        protected void Button_Click(object sender SystemEventArgs e)
        {
            int ncount = GetRecordCount();
            if (ncount == )
            {
                KunslyWebPageShowMessage(this請先創建相冊才允許上傳圖片);
                return;
            }
            //    ResponseWrite(<script language=javascript>alert(請先創建相冊才允許上傳圖片);historyback()</script>);
            //else
            //{
            if(UploadFilePostedFileFileNameTrim() == 
            
                //ResponseWrite(<script language=javascript>alert(老大沒文件怎麼傳啊);historyback()</script>);
                KunslyWebPageShowMessage(this請選擇文件);
                return;
            } 
            //    else
            //    {
            if (txtMessageTextTrim() == )
            {
                KunslyWebPageShowMessage(this請輸入簡介);
                return;            
            }
                //ResponseWrite(<script language=javascript>alert(老大請輸入簡介);historyback()</script>);
            extension = PathGetExtension(UploadFilePostedFileFileName)ToUpper(); 
            
            if (extension != JPG && extension != GIF)
            {
                KunslyWebPageShowMessage(this非法文件不允許上傳只允許上傳jpg或gif文件);
                return;            
            }
            //ResponseWrite(<script language=javascript>alert(非法文件不允許上傳);historyback()</script>);
            if (UploadFilePostedFileContentLength >= ) //大於M不允許上傳
            {
                KunslyWebPageShowMessage(this文件過大不能上傳單個文件最大不超過M);
                return;            
            }
            fileName = DateTimeNowToString(yyyyMMddhhmmss); 
            path = ServerMapPath() + /upfile/ + fileName + extension; 
            updata();
        }    
                
                    
            
        


        void updata()#region void updata()
        void updata()
            {
            //這個過程可以更新的數據庫把圖片地址插入到數據庫
            UploadFilePostedFileSaveAs(path); 
            int widthheightnewwidth=newheight; 
            SystemDrawingImage image;
            image =  SystemDrawingImageFromFile(path);
            width=imageWidth;
            height=imageHeight;
            newheight=height*newwidth/width;
            Bitmap bm = new Bitmap(imagenewwidth newheight);
            bmSave(ServerMapPath() + /upfile/ + fileName + _s + extension ImageFormatJpeg);
            image =  SystemDrawingImageFromFile(path);
            LblShowPhotoText = 照片已經成功上傳文件名為 + fileName + extension +  占用空間;
            LblShowPhotoText += UploadFilePostedFileContentLength/ + k;
            string strtxtMessage = txtMessageText;
            strtxtMessage = strtxtMessageReplace();
            strtxtMessage = strtxtMessageReplace();
            strtxtMessage = strtxtMessageReplace( <br>);
            string strSql = insert into photourl ;
            strSql += (PhotoUrlPhotoUrl_sPhotoWidthPhotoHeightPhotoSizePuttimePhotoIntro;
            strSql += PhotoCollMobilePhotoColl);
            strSql +=  values ;
            strSql += (+fileName+extension++fileName + _s + extension++imageWidth++imageHeight;
            strSql += +UploadFilePostedFileContentLength+getdate()+strtxtMessage;
            strSql += +DdlPhotoTypeSelectedValueToString()Trim()++smobile+;
            strSql += DdlPhotoCollSelectedValueToString()Trim()+);
            SqlConnection dbConn = DbConnectionGetSqlConn();
            SqlCommand dbCmd = new SqlCommand(strSql dbConn);
            dbCmdExecuteNonQuery();
            dbCmdDispose();
            dbConnClose();
            imageDispose();
            bmDispose();


            }
        #endregion


        void DropDownPhotoType()#region void DropDownPhotoType()
        void DropDownPhotoType()
        {
            //這個過程是將照片類型綁定
            string strSql = select * from photocolltype;
            SqlConnection dbConn = DbConnectionGetSqlConn();
            SqlCommand dbCmd = new SqlCommand(strSql dbConn);
            SqlDataReader dbReader = dbCmdExecuteReader();
            DdlPhotoTypeItemsClear();
            while (dbReaderRead())
            {
                DdlPhotoTypeItemsAdd(new ListItem(dbReader[CollType]ToString()dbReader[PhotoID]ToString()));

            }
            dbReaderClose();
            dbConnClose();
        }
        #endregion

        void DropDownPhotoColl()#region void DropDownPhotoColl()
        void DropDownPhotoColl()
        {
            //這個過程是將相冊類型綁定            
            string strSql = select * from PhotoColl where mobile= ++smobile+;
            SqlConnection dbConn = DbConnectionGetSqlConn();
            SqlCommand dbCmd = new SqlCommand(strSql dbConn);
            SqlDataReader dbReader = dbCmdExecuteReader();
            DdlPhotoCollItemsClear();
            while (dbReaderRead())
            {
                DdlPhotoCollItemsAdd(new ListItem(dbReader[PhotoColl]ToString()dbReader[PhotoID]ToString()));
                

            }
            
                
            dbReaderClose();
            dbConnClose();
        }
        #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 = DbConnectionGetSqlConn();
            SqlCommand dbCmd = new SqlCommand(strSql dbConn);
            SqlDataReader dbReader = dbCmdExecuteReader();

            int nCount = ;
            if(dbReaderRead())
                nCount = ConvertToInt(dbReader[tt]);
            
            dbReaderClose();
            dbConnClose();
            return nCount;
        }

        #endregion 



    }
}

From:http://tw.wingwit.com/Article/program/net/201311/12300.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.