一次上传多个图片

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ployImg_add.aspx.cs" Inherits="admin_ployImg_add" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <link href="../admin/css/admin_style.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
   var i=1
   function addFile()
   {
  
   if (i<8)
   {
   var str = '<br /><br />&nbsp;<input type="file" name="File" runat="server" style="width: 300px"/>&nbsp;描述:&nbsp;<input name="text" type="text" style="width: 300px" maxlength="20" />'
   document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
  
   }
   else
   {
   alert("您一次最多只能上传8张图片!")
   }
   i++
   }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div class="head">
            活动详细内容管理</div>
   
    </div>
        <div class="div">
            <table style="width: 100%" bordercolor="#7acbc7" border="1" cellpadding="0" cellspacing="0" class="table">
                <tr>
                    <td style="width: 200px; height: 30px">
                        图片主题:</td>
                    <td style="width: 760px; height: 30px" align="left">
                        &nbsp;<asp:TextBox ID="TextBox1" runat="server" Width="60%"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
                            Display="Dynamic" ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator></td>
                </tr>
                <tr>
                    <td style="width: 200px; height: 30px">
                        所属活动:</td>
                    <td style="width: 760px; height: 30px" align="left">
                        &nbsp;<asp:DropDownList ID="DropDownList1" runat="server" Width="250px">
                        </asp:DropDownList>
                        <asp:Label ID="lblMessage" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label></td>
                </tr>
                <tr>
                    <td style="width: 200px; height: 30px">
                        所属学校:</td>
                    <td align="left" style="width: 760px; height: 30px">
                        &nbsp;<asp:DropDownList ID="DropDownList2" runat="server" Width="250px">
                        </asp:DropDownList></td>
                </tr>
                <tr>
                    <td style="width: 200px; height: 30px">
                        活动图片:</td>
                    <td style="width: 760px; height: 30px" align="left">
                        &nbsp;<br />
                        &nbsp;<input id="Button2" class="bluebuttoncss" οnclick="addFile()" type="button"
                            value="增加图片(Add)" /><br />
                        <p id="MyFile">
                        &nbsp;<input id="File1" runat="server" name="File" style="width: 300px" type="file" />&nbsp;描述:
                        <input maxlength="20" name="text" style="width: 300px" type="text" /></p></td>
                </tr>
                <tr>
                    <td colspan="2" style="width: 100%; height: 30px">
                        <asp:Button ID="Button1" runat="server" CssClass="bluebuttoncss" Text=" 添 加 " OnClick="Button1_Click" />
                        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                        &nbsp; &nbsp;&nbsp;
                        <input id="Reset1" class="bluebuttoncss" type="reset" value=" 重 填 " /></td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using schoolBLL;
using schoolModels;
using System.Collections.Generic;
using System.IO;

public partial class admin_ployImg_add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SchoolLogin schoolLogin = Session["AdminUser"] as SchoolLogin;
        if (schoolLogin == null)
        {
            Response.Redirect("login.aspx");
        }
        else
        {
            if (!IsPostBack)
            {
                switch(schoolLogin.SType.ToString())
                {
                    case "4":  //学校管理员
                        IList<PloyInfo> list = PloyInfoManager.GetPloyInfoBySchool(schoolLogin.SchoolId);
                        this.DropDownList1.Items.Clear();
                        for (int i = 0; i < list.Count; i++)
                        {
                            this.DropDownList1.Items.Add(new ListItem(list[i].Title,list[i].Id.ToString()));
                        }
                        this.DropDownList2.Items.Clear();
                        this.DropDownList2.Items.Add(new ListItem(SchoolInfoManager.GetSchoolInfoById(schoolLogin.SchoolId).Name,schoolLogin.SchoolId.ToString()));
                        break;
                    case "0":
                        IList<PloyInfo> listAll = PloyInfoManager.GetPloyInfoAll();
                        this.DropDownList1.Items.Clear();
                        for (int i = 0; i < listAll.Count; i++)
                        {
                            this.DropDownList1.Items.Add(new ListItem(listAll[i].Title, listAll[i].Id.ToString()));
                        }
                        this.DropDownList2.Items.Clear();
                        IList<SchoolInfo> listSC = SchoolInfoManager.GetSchoolInfoAll();
                        for (int j = 0; j < listSC.Count; j++)
                        {
                            this.DropDownList2.Items.Add(new ListItem(listSC[j].Name, listSC[j].Id.ToString()));
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";
        lblMessage.Visible = false;
        System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
        System.Text.StringBuilder strmsg = new System.Text.StringBuilder("");
        string[] rd = Request.Form["text"].Split(',');//获得图片描述的文本框字符串数组,为对应的图片的描述
        string albumid = this.DropDownList1.SelectedValue.Trim();  //所属活动
        int ifile;
        for (ifile = 0; ifile < files.Count; ifile++)
        {
            if (files[ifile].FileName.Length > 0)
            {
                System.Web.HttpPostedFile postedfile = files[ifile];
                if (postedfile.ContentLength / 1024 > 1024)//单个文件不能大于1024k
                {
                    strmsg.Append(Path.GetFileName(postedfile.FileName) + "---不能大于1024k<br>");
                    break;
                }
                string fex = Path.GetExtension(postedfile.FileName);
                if (fex != ".jpg" && fex != ".JPG" && fex != ".gif" && fex != ".GIF")
                {
                    strmsg.Append(Path.GetFileName(postedfile.FileName) + "---图片格式不对,只能是jpg或gif<br>");
                    break;
                }
            }
        }
        if (strmsg.Length <= 0)//说明图片大小和格式都没问题
        {
            //以下为创建图库目录
            //string dirname = "pic00" + ddlAlbum.SelectedValue.Trim();
            string dirpath = Server.MapPath("~/images/");
            //dirpath = dirpath;  // + @"/" + dirname
            if (Directory.Exists(dirpath) == false)
            {
                Directory.CreateDirectory(dirpath);
            }
            Random ro = new Random();
            int count = 0;
            int name = 1;
            for (int i = 0; i < files.Count; i++)
            {
                System.Web.HttpPostedFile myFile = files[i];
                string FileName = "";
                string FileExtention = "";
                string PicPath = "";
                FileName = System.IO.Path.GetFileName(myFile.FileName);
                string stro = ro.Next(100, 100000000).ToString() + name.ToString();//产生一个随机数用于新命名的图片
                string NewName = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + stro;
                if (FileName.Length > 0)//有文件才执行上传操作再保存到数据库
                {
                    FileExtention = System.IO.Path.GetExtension(myFile.FileName);

                    string ppath = dirpath + @"/" + NewName + FileExtention;
                    myFile.SaveAs(ppath);
                    string FJname = FileName;
                    //PicPath = "images" + @"/" + dirname + @"/" + NewName + FileExtention;
                    PicPath = "images" + @"/" + NewName + FileExtention;

                }
                /* 调用方法 */
                count += AddPicture(this.TextBox1.Text.Trim().ToString(), albumid, NewName + FileExtention, rd[i],Convert.ToInt32(this.DropDownList2.SelectedValue));//将图片信息保存到数据库
                name = name + 1;//用来重命名规则的变量

            }
            if (count > 0)
            {
                Communal.ShowMessage("添加成功!", "ployImg_show.aspx");
            }
            else
            {
                Communal.ShowMessage("添加失败!","ployImg_add.aspx");
            }
        }
        else
        {
            lblMessage.Text = strmsg.ToString();
            lblMessage.Visible = true;
        }
    }
    private int AddPicture(string title,string piId,string image,string text,int schoolId)
    {
        SchoolLogin schoolLogin = Session["Adminuser"] as SchoolLogin;
        PloyImgInfo ployImg = new PloyImgInfo();
        ployImg.Title = title;
        ployImg.SchoolId = schoolId;
        ployImg.UpdateId = schoolLogin.Sid;
        ployImg.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        ployImg.PlId = PloyInfoManager.GetPloyInfoById(Convert.ToInt32(piId));
        ployImg.Iamges = image;
        ployImg.Pibewrite = text;
        return PloyImgInfoManager.AddPloyImgInfo(ployImg);  
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

仿秀达人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值