国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

我寫的上傳(upload)文件的codebehind代碼

2024-07-21 02:16:57
字體:
供稿:網(wǎng)友
菜鳥學(xué)堂:
功能:
1。把圖片文件(jpg gif png)上傳,
2。保存到指定的路徑(在web.config中設(shè)置路徑,以文件的原有格式保存),
3。并自動(dòng)生成指定寬度的(在web.config中設(shè)置寬度)
4。和指定格式的(在web.config中指定縮略圖的格式)
5。和原圖比例相同的縮略圖(根據(jù)寬度和原圖的寬和高計(jì)算所略圖的高度)
6。可以判斷是否已經(jīng)存在文件
7。如果不覆蓋,則給出錯(cuò)誤
8。如果選中"覆蓋原圖"checkbox,則覆蓋原圖。
9。可以根據(jù)要求,在webform上設(shè)置1個(gè)以上的file input和相應(yīng)的checkbox
10。并在文件上傳完畢后,顯示原圖的文件名,尺寸,字節(jié),和
11。縮略圖的文件名尺寸。
12。縮略圖的文件名格式:原圖+"_thumb."+指定格式,如:test.jpg_thumb.gif,以便于管理。

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

                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();
                
                    imgnameonly = 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>錯(cuò)誤:</b>文件("+ (i+1) +") " + imgnameonly + " 已經(jīng)存在,請(qǐng)修改文件名<br>" );
                        }
                    }
                    else
                    {
                        erronumber = erronumber + 1;
                        picinfo.append("<b>錯(cuò)誤:</b>文件("+ (i+1) +") " + imgnameonly + " 擴(kuò)展名 " + fileext + " 不被許可<br>" );
                    }                

                }

                if(erronumber > 0)
                {
                    picinfo.append("<font color=red>全部操作均未完成,請(qǐng)修改錯(cuò)誤,再進(jìn)行操作</font><br>");
                }
                else            
                {
                    for(int i = 0;i < request.files.count; i++)
                    {
                
                        httppostedfile postedfile = request.files[i];
                        imgnameonly = 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>");
                    
                        oriimg.dispose();
                        newimg.dispose();                
                    
                    }//for
                    picinfo.append("<font color=red>所有操作成功</font><br>");

                }// if erronumber = 0

            
                
            }
            else
            {
                picinfo.append("<font color=red>有錯(cuò)誤,請(qǐng)檢查。操作未成功</font><br>");

            }

            lblpicinfo.text = picinfo.tostring();

        }
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 梅河口市| 乌苏市| 永仁县| 安福县| 桑植县| 什邡市| 祁连县| 阳新县| 响水县| 柳河县| 凤山县| 汶川县| 印江| 台中市| 赫章县| 白山市| 饶阳县| 沾化县| 微山县| 西乡县| 永登县| 台南县| 石家庄市| 金乡县| 东台市| 兰溪市| 泗水县| 古丈县| 六枝特区| 长宁区| 台州市| 浙江省| 宜君县| 岳阳市| 金堂县| 隆尧县| 大足县| 延长县| 同德县| 拉萨市| 海宁市|