//页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddProImages.aspx.cs" Inherits="Opalsoft.UB.Web.Manager.Product.AddProImages" %>
<!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>
<script language="javascript" type="text/javascript">
var i = 0;
function Dom() {
var message = document.getElementById("message");
if (i == 5) {
message.innerHTML = "不能再添加更多的图片了";
return;
}
var div = document.createElement("div");
var div1 = document.createElement("div");
var div2 = document.createElement("div");
div.setAttribute("style", "width: 500px;");
div1.setAttribute("style", "width: 250px; float: left;");
div2.setAttribute("style", "width: 220px; float: left;");
var textCon = document.createElement("input");
var textControl = document.createElement("input");
textControl.setAttribute("type", "file");
textControl.setAttribute("name", "fileImage");
textCon.setAttribute("type", "text");
textCon.setAttribute("name", "txtName");
div1.appendChild(textControl);
div2.appendChild(textCon);
div.appendChild(textControl);
div.appendChild(textCon);
document.getElementById("Show").appendChild(div);
i += 1;
}
function load() {
var div = document.createElement("div");
var div1 = document.createElement("div");
var div2 = document.createElement("div");
div.setAttribute("style", "width: 800px;");
div1.setAttribute("style", "width: 250px; float: left;");
div2.setAttribute("style", "width: 220px; float: left;");
var textCon = document.createElement("input");
var textControl = document.createElement("input");
textControl.setAttribute("type", "file");
textControl.setAttribute("name", "fileImage");
textCon.setAttribute("type", "text");
textCon.setAttribute("name", "txtName");
div1.appendChild(textControl);
div2.appendChild(textCon);
div.appendChild(textControl);
div.appendChild(textCon);
document.getElementById("Show").appendChild(div);
//document.getElementById("show2").appendChild(textCon);
}
</script>
</head>
<body οnlοad="load();" style="font-size: 12px; padding-top: 0px; padding-left: 0px;">
<form id="form1" enctype="multipart/form-data" runat="server">
<div style="background-image: url('../Images/beijing_03.gif'); width: 100%; background-repeat: repeat-y">
<div>
<span style="font-size: 12px; color: #005097">
<img src="../images/nav.gif" border="0" />后台管理>> 添加产品>> 添加图片 </span>
</div>
<div>
<a href="javascript:Dom();">在添加一个</a> <span id="message" style="color: Red;"></span>
</div>
<div id="Show" style="width: 500px;">
<div>
<div style="width: 200px; float: left" id="show1">
路径
</div>
<div style="width: 220px; float: left" id="show2">
描述
</div>
</div>
</div>
<div>
<div style="width: 220px;">
<asp:Label runat="server" ID="lblMeaage" ForeColor="Red" />
</div>
<div style="margin-left: 20px;">
<asp:ImageButton runat="server" ID="btnOk" ImageUrl="../Images/anniu_03.gif"
OnClick="btnOk_Click" />
</div>
</div>
</div>
</form>
</body>
</html>
//后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Opalsoft.UB.Model;
using System.IO;
using Opalsoft.UB.BLL;
namespace Opalsoft.UB.Web.Manager.Product
{
public partial class AddProImages : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["proId"] == null)
{
new Exception("操作出错");
}
ViewState["proId"] = Request.QueryString["proId"];
}
}
protected void btnOk_Click(object sender, ImageClickEventArgs e)
{
//获得创建文本框的值
int proId = int.Parse(ViewState["proId"].ToString());
//产生一个随机数用于新命名的图片
Random ro = null;
string text = System.Web.HttpContext.Current.Request["txtName"].ToString();
List<ProImages> proImages = new List<ProImages>();
ProImages proIgm = null;
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
for (int ifile = 0; ifile < files.Count; ifile++)
{
if (files[ifile].FileName.Length > 0)
{
System.Web.HttpPostedFile postedfile = files[ifile];
if (postedfile.ContentLength / 1024 > 1024)
{
this.lblMeaage.Text = "单个文件不能大于1024k";
return;
}
string fex = Path.GetExtension(postedfile.FileName);
if (fex != ".jpg" && fex != ".JPG" && fex != ".gif" && fex != ".GIF")
{
this.lblMeaage.Text = "图片格式不对,只能是jpg或gif";
return;
}
}
else {
this.lblMeaage.Text = "请选择图片后在上传!";
return;
}
}
string[] rd = text.Split(','); ;
for (int i = 0; i < files.Count; i++)
{ //获得图片描述的文本框字符串数组,为对应的图片的描述
proIgm = new ProImages();
ro = new Random();
string stro = ro.Next(100, 100000000).ToString();
string NewName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + "UB_Pro_" + stro + i.ToString();
HttpPostedFile file = files[i];
string Imagepath = Server.MapPath("../../Image/ProductsImages/" + proId.ToString() + "/" + NewName + Path.GetExtension(file.FileName));
if (!Directory.Exists(Server.MapPath("../../Image/ProductsImages/" + proId.ToString() + "/")))
{
Directory.CreateDirectory(Server.MapPath("../../Image/ProductsImages/" + proId.ToString() + "/"));
}
file.SaveAs(Imagepath);
proIgm.pro_id = proId;
proIgm.status = "A";
proIgm.image_description = rd[i];
proIgm.image_url = "Image/ProductsImages/" + proId.ToString() + "/" + NewName + Path.GetExtension(file.FileName);
proImages.Add(proIgm);
}
new BidManagerBLL().AddProImages(proImages);
this.lblMeaage.Text = "添加成功";
}
}
}