首页 > 开发 > 综合 > 正文

图片上传的Codebehind(自动生成所略图)

2024-07-21 02:25:15
字体:
来源:转载
供稿:网友
using system;
using system.collections;
using system.componentmodel;
using system.configuration;
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.text;
using cj168.util;



namespace cj168.web.mag.admins.fedata
{
    /// <summary>
    /// summary description for fedata.
    /// </summary>
    public class feuploadpic : system.web.ui.page
    {
        protected system.web.ui.webcontrols.textbox txtboxpictitle;
        protected system.web.ui.webcontrols.calendar calpicdate;
        protected system.web.ui.webcontrols.dropdownlist ddlpictype;
        protected system.web.ui.webcontrols.textbox txtboxpicintro;
        protected system.web.ui.htmlcontrols.htmlinputfile filepicname;
        protected system.web.ui.webcontrols.button btnsubmit;
        protected system.web.ui.webcontrols.label lblpicinfo;
        protected system.web.ui.webcontrols.textbox txtboxpicdate;
        protected system.web.ui.webcontrols.requiredfieldvalidator requiredfieldvalidator1;
        protected system.web.ui.webcontrols.regularexpressionvalidator vldcatname;
        protected system.web.ui.webcontrols.requiredfieldvalidator requiredfieldvalidator2;
        protected system.web.ui.webcontrols.customvalidator customvalidator1;
        protected system.web.ui.webcontrols.regularexpressionvalidator regularexpressionvalidator1;
        protected system.web.ui.webcontrols.hyperlink hlkoripic;
        protected system.web.ui.webcontrols.hyperlink hlknewpic;
        protected system.web.ui.webcontrols.regularexpressionvalidator regularexpressionvalidator2;
        protected system.web.ui.webcontrols.regularexpressionvalidator regularexpressionvalidator3;
        protected system.web.ui.webcontrols.checkboxlist checkboxlistrewrite;
        
        
    
        private void page_load(object sender, system.eventargs e)
        {
            // put user code to initialize the page here
            //if(page.ispostback)
            //txtboxpicdate.text = calpicdate.selecteddate.tostring();

            if(!page.ispostback)
            {
                bind2dropdownlistpictype();

                //for(int i = 0;i < request.files.count; i++)
                //{                
                //    checkboxlistrewrite.items[i].selected = false;
                //}
            }


            


        }

        public void bind2dropdownlistpictype()
        {
            cj168.dataaccess.admins admins = new cj168.dataaccess.admins();
            dataset ds = admins.fepictypeget();
            //ddlpictype.datasource = ;
            dataview dv = new dataview(ds.tables["pictype"]);
            
            datarowview drv = dv.addnew();
            //datacolumnview dcv = dv.addnew();
            drv["title"] = "请选择";
            drv["fepictypeid"] = "0";
            drv.endedit();            
            

            dv.sort = "fepictypeid";            

            ddlpictype.datasource = dv;

            ddlpictype.databind();
        }

        public void uploadfile(object sender, system.eventargs e)
        {
            
                string imgnameonly, imgnamenoext, imgext;
                string imgthumbnail;
                int erronumber = 0;
                system.drawing.image oriimg, newimg;
                string strfepicsavepath = configurationsettings.appsettings["fepicsavepath"].tostring();
                string strfepicthumbformat = configurationsettings.appsettings["fepicthumbformat"].tostring().tolower();
                int intfethumbwidth = int32.parse(configurationsettings.appsettings["fepicthumbwidth"]);
                string fileext;
                string strpictitle = txtboxpictitle.text;
                string strpicintro = txtboxpicintro.text;
                string strpicdate = txtboxpicdate.text;

                int intpictype = int32.parse(ddlpictype.selecteditem.value);
                string strpictype = intpictype.tostring() + "_" + datetime.now.date.toshortdatestring() + "_";
                string strfepicwebpath = configurationsettings.appsettings["fepicwebpath"];

                cj168.dataaccess.admins admins = new cj168.dataaccess.admins();
                
                //if(admins.fepictypeset(textboxtitle.text,0) < 0)
                //    lbladdpicinfo.text = "操作失败:已经存在相同名称类型,请修改";
                //else
                //    lbladdpicinfo.text = "操作成功";


                stringbuilder picinfo = new stringbuilder();

            
            
            if(page.isvalid)
            {
            

                for(int i = 0;i < request.files.count; i++)
                {
                    httppostedfile postedfile = request.files[i];
                    fileext = (system.io.path.getextension(postedfile.filename)).tostring().tolower();
                    //5-test.jpg
                    imgnameonly = strpictype + system.io.path.getfilename(postedfile.filename);
                    if(fileext == ".jpg" || fileext == ".gif" || fileext == ".png")
                    {

                        if(system.io.file.exists(strfepicsavepath + imgnameonly) && (checkboxlistrewrite.items[i].selected == false))
                        {
                            erronumber = erronumber + 1;
                            picinfo.append("<b>错误:</b>文件("+ (i+1) +") " + imgnameonly + " 已经存在,请修改文件名<br>" );
                        }
                    }
                    else
                    {
                        erronumber = erronumber + 1;
                        if(fileext == "")
                            picinfo.append("<b>错误:</b>请选择文件<br>" );
                        else
                        picinfo.append("<b>错误:</b>文件("+ (i+1) +") " + imgnameonly + " 扩展名 " + fileext + " 不被许可<br>" );
                    }                

                }

                if(erronumber > 0)
                {
                    picinfo.append("<font color=red>全部操作均未完成,请修改错误,再进行操作</font><br>");

                    hlkoripic.imageurl = "";
                    hlkoripic.tooltip = "";
                    hlknewpic.imageurl = "";
                    hlknewpic.tooltip = "";
                }
                else            
                {
                    for(int i = 0;i < request.files.count; i++)
                    {
                
                        httppostedfile postedfile = request.files[i];
                        imgnameonly = strpictype + system.io.path.getfilename(postedfile.filename);
                        imgnamenoext = system.io.path.getfilenamewithoutextension(postedfile.filename);
                        imgext = system.io.path.getextension(postedfile.filename).tostring().tolower();
                    
                    
                        oriimg = system.drawing.image.fromstream(postedfile.inputstream);
                        newimg = oriimg.getthumbnailimage(intfethumbwidth, intfethumbwidth * oriimg.height/oriimg.width,null,new system.intptr(0));
                        switch(imgext)
                        {
                            //case ".jpeg":
                            case ".jpg":
                                oriimg.save(strfepicsavepath + imgnameonly , system.drawing.imaging.imageformat.jpeg);
                                break;
                            case ".gif":
                                oriimg.save(strfepicsavepath + imgnameonly , system.drawing.imaging.imageformat.gif);
                                break;
                            case ".png":
                                oriimg.save(strfepicsavepath + imgnameonly , system.drawing.imaging.imageformat.png);
                                break;
                        }
                        
                        //oriimg.save(configurationsettings.appsettings["fepicsavepath"] + imgnamenoext + ".jpg", system.drawing.imaging.imageformat.jpeg);
                        
                                        
                        switch(strfepicthumbformat)
                        {
                                //jpeg format can get the smallest file size, and the png is the largest size
                            //case "jpeg":
                            case "jpg":
                                newimg.save(strfepicsavepath + imgnameonly + "_thumb.jpg",system.drawing.imaging.imageformat.jpeg);
                                imgthumbnail = imgnameonly + "_thumb.jpg";
                                break;
                            case "gif":
                                newimg.save(strfepicsavepath + imgnameonly + "_thumb.gif",system.drawing.imaging.imageformat.gif);
                                imgthumbnail = imgnameonly + "_thumb.gif";
                                break;
                            case "png":
                                newimg.save(strfepicsavepath + imgnameonly + "_thumb.png",system.drawing.imaging.imageformat.png);
                                imgthumbnail = imgnameonly + "_thumb.png";
                                break;
                            default:
                                newimg.save(strfepicsavepath + imgnameonly + "_thumb.jpg",system.drawing.imaging.imageformat.jpeg);
                                imgthumbnail = imgnameonly + "_thumb.jpg";                        
                                break;
                    
                        }//switch
                        
                        picinfo.append("<b>文件 名:</b>" + imgnameonly + " ( " + oriimg.width + " x " + oriimg.height + " ) " + postedfile.contentlength/1024 + "kb<br>");
                        picinfo.append("<b>缩略图名:</b>" + imgthumbnail + " ( " + newimg.width + " x " + newimg.height + " )<br><br>");

                        hlkoripic.imageurl = strfepicwebpath + imgnameonly;
                        hlkoripic.tooltip = "◆原图◆/n文件名:" + imgnameonly + "/n尺寸:" + oriimg.width + " x " + oriimg.height + "/n字节:" + postedfile.contentlength/1024 + "kb";
                        hlknewpic.imageurl = strfepicwebpath + imgthumbnail;
                        hlknewpic.tooltip = "◆缩略图◆/n文件名:" + imgthumbnail + "/n尺寸:" + newimg.width + " x " + newimg.height;
                        

                        oriimg.dispose();
                        newimg.dispose();
                        picinfo.append("<font color=red>图片上传成功</font><br>");
                        if(admins.fepicdataset(strpictitle, strpicdate, imgnameonly, intpictype, strpicintro, imgthumbnail,0) < 0)
                            picinfo.append("<font color=red>保存信息到数据库失败</font><br>");
                        else
                            picinfo.append("<font color=red>保存信息到数据库成功</font><br>");
                    
                    }//for
                    picinfo.append("<font color=red>所有操作成功</font><br>");

                }// if erronumber = 0

            
                
            }
            else
            {
                picinfo.append("<font color=red>有错误,请检查。操作未成功</font><br>");

                hlkoripic.imageurl = "";
                hlkoripic.tooltip = "";
                hlknewpic.imageurl = "";
                hlknewpic.tooltip = "";

            }

            for(int i = 0;i < request.files.count; i++)
            {                
                checkboxlistrewrite.items[i].selected = false;
            }

            lblpicinfo.text = picinfo.tostring();

        }

        public void caldateselected(object sender,system.eventargs e)
        {
            txtboxpicdate.text = calpicdate.selecteddate.toshortdatestring();

        }

        public void servervalidatecheckdate(object sender, system.web.ui.webcontrols.servervalidateeventargs value)
        {
            cj168.util.iutil iutils = new cj168.util.iutil();
            if(!iutils.isdate(value.value))
                value.isvalid = false;
            else
                value.isvalid = true;

        }

        


        #region web form designer generated code
        override protected void oninit(eventargs e)
        {
            //
            // codegen: this call is required by the asp.net web form designer.
            //
            initializecomponent();
            base.oninit(e);
        }
        
        /// <summary>
        /// required method for designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void initializecomponent()
                                                                    {    
            this.load += new system.eventhandler(this.page_load);

        }
        #endregion
    }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表