dev-club上C.T的大作.
转贴<%@ Page language="c#" Codebehind="feUploadPic.aspx.cs" AutoEventWireup="false" Inherits="cj168.Web.Mag.Admins.FeData.feUploadPic" %>
<%@ Register TagPrefix="cj168" TagName="Header" Src="modules/headerFe.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>fedata</title>
        <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
        <meta content="C#" name="CODE_LANGUAGE">
        <meta content="JavaScript" name="vs_defaultClientScript">
        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
        <style>BODY { FONT-SIZE: 9pt }
    .calPicDate { FONT-SIZE: 9pt; FONT-FAMILY: Verdana, Helvetica, sans-serif }
    .valid { FONT-SIZE: 9pt; COLOR: red }
    TD { FONT-SIZE: 9pt }
        </style>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <table width="100%">
            <tr>
                <td><cj168:header id="Header1" runat="server"></cj168:header></td>
            </tr>
        </table>
        <form id="fedata" method="post" encType="multipart/form-data" runat="server">
            <TABLE id="Table1" cellSpacing="0" cellPadding="1" width="750" border="1">
                <TR>
                    <TD width="70">图片标题</TD>
                    <TD><asp:textbox id="txtboxPicTitle" runat="server"></asp:textbox><br>
                        <asp:requiredfieldvalidator id="Requiredfieldvalidator1" runat="server" ErrorMessage="请填写图片标题。" CssClass="valid" ControlToValidate="txtboxPicTitle"></asp:requiredfieldvalidator><br>
                        <asp:regularexpressionvalidator id="vldCatName" ErrorMessage="图片标题至少2个字符,不应包含'“ ” ! @ # $ % ^ & * ( ) < > 《 》{ } [ ] ? 。,? ¥" CssClass="valid" ControlToValidate="txtboxPicTitle" ValidationExpression="[^'^“^”^\^!^@^#^$^%^\^^&^*^(^)^<^>^《^》^?^。^,^?^¥^\{^\}^\][^\]]{2,}" Runat="server"></asp:regularexpressionvalidator></TD>
                </TR>
                <TR>
                    <TD>图片日期</TD>
                    <TD><asp:textbox id="txtboxPicDate" runat="server" Width="300"></asp:textbox><asp:calendar id="calPicDate" runat="server" CssClass="calPicDate" Width="300" SelectMonthText="本月" SelectWeekText="本周" NextPrevFormat="FullMonth" OnSelectionChanged="CalDateSelected">
                            <NextPrevStyle CssClass="calPicDate"></NextPrevStyle>
                            <WeekendDayStyle ForeColor="red"></WeekendDayStyle>
                            <TodayDayStyle Font-Bold="True" ForeColor="red" BackColor="#ccccff"></TodayDayStyle>
                            <SelectedDayStyle BackColor="black" Font-Bold="true"></SelectedDayStyle>
                        </asp:calendar><BR>
                        <asp:requiredfieldvalidator id="Requiredfieldvalidator2" runat="server" ErrorMessage="请选择或填写图片日期。" CssClass="valid" ControlToValidate="txtboxPicDate"></asp:requiredfieldvalidator><br>
                        <asp:customvalidator id="CustomValidator1" ErrorMessage="不是个有效的日期格式" CssClass="valid" ControlToValidate="txtboxPicDate" Runat="server" OnServerValidate="ServerValidateCheckDate"></asp:customvalidator></TD>
                </TR>
                <TR>
                    <TD>图片分类</TD>
                    <TD><asp:dropdownlist id="ddlPicType" runat="server" DataValueField="fePicTypeID" DataTextField="Title"></asp:dropdownlist><FONT face="宋体"> </FONT><asp:regularexpressionvalidator id="RegularExpressionValidator1" ErrorMessage="请选择图片分类" CssClass="valid" ControlToValidate="ddlPicType" ValidationExpression="[^0]{1,}" Runat="server"></asp:regularexpressionvalidator></TD>
                </TR>
                <TR>
                    <TD>图片介绍</TD>
                    <TD><asp:textbox id="txtboxPicIntro" runat="server" MaxLength="255" TextMode="MultiLine" Columns="50" Rows="5"></asp:textbox>
                        <asp:RegularExpressionValidator id="Regularexpressionvalidator2" Runat="server" CssClass="valid" ErrorMessage="内容中不应包含 ' <  >" ControlToValidate="txtboxPicIntro" ValidationExpression="[^'^<^>]{0,}"></asp:RegularExpressionValidator>
                    </TD>
                </TR>
                <tr>
                    <td colSpan="2">(支持文件格式:Jpg, Gif, Png)</td>
                </tr>
            </TABLE>
            <table cellSpacing="0" width="750" border="1">
                <tr>
                    <td width="70">图片名称</td>
                    <td width="300"><INPUT title="浏览" type="file" size="25" name="filePicName" runat="server" ID="filePicName">
                        <asp:RegularExpressionValidator id="Regularexpressionvalidator3" Runat="server" CssClass="valid" ErrorMessage="文件名只能由字母或数字组成,不能包含 - _ 等其他符号长度至少为1" ControlToValidate="filePicName" ValidationExpression="[ :.\\a-zA-Z0-9]{1,}"></asp:RegularExpressionValidator>
                    </td>
                    <td><asp:checkboxlist id="checkboxlistRewrite" runat="server" BorderWidth="0" Height="100%" CellPadding="5" RepeatLayout="Table" RepeatColumns="1" RepeatDirection="Vertical">
                            <asp:ListItem Value="1">覆盖原有图片</asp:ListItem>
                        </asp:checkboxlist></td>
                </tr>
            </table>
            <table cellSpacing="0" width="750" border="1">
                <TR>
                    <TD align="middle"><asp:button id="btnSubmit" onclick="UploadFile" runat="server" Text="确定"></asp:button></TD>
                </TR>
                <TR>
                    <TD><asp:label id="lblPicInfo" runat="server">Label</asp:label></TD>
                </TR>
                <TR>
                    <TD><FONT face="宋体"></FONT>
                    </TD>
                </TR>
            </table>
        </form>
        <asp:HyperLink id="hlkOriPic" runat="server">原图</asp:HyperLink>
        <asp:HyperLink id="hlkNewPic" runat="server">缩略图</asp:HyperLink>
    </body>
</HTML>

解决方案 »

  1.   

    feUploadPic.aspx.cs(第一部分)
    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();
            }
      

  2.   

    feUploadPic.aspx.cs(第二部分)
        
            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>" );
                        }                                }
      

  3.   

    http://www.dev-club.com/club/bbs/ShowAnnounce.asp?v=&ID=1181058
    自已去看吧.
    CSDN真是麻烦,发贴不但有长度限制还不能连续回复三次以上.
      

  4.   

    不用这么麻烦吧,用几行代码就可以了:
    System.Drawing.Image image = System.Drawing.Image.FromFile(src);
    //src是原来的图片的路径
    System.Drawing.Image aNewImage = image.GetThumbnailImage(iWidth, iHeight, null, new System.IntPtr());
    //iWidth,iHeight是你想要得新的图片的宽度和高度
    aNewImage.Save(Server.MapPath(fileName));
    //fileName是新的图片的文件名
    image.Dispose();
      

  5.   

    楼上的兄弟说的好:)
    不过这样只能当PNG图片保存啊!
    能不能保存为其他类型的图片呢?