服务端代码:
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 System.IO;
using Business;
public partial class upload : BasePage
{
public static int j = 0;
public static int k = 0;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_add_Click(object sender, EventArgs e)
{
FileUpload fu;
j = int.Parse(TextBox1.Text.Trim());
k = int.Parse(TextBox1.Text.Trim());
for (int i = 0; i < Convert.ToInt32(TextBox1.Text); i++)
{
fu = new FileUpload();
fu.ID = "fu" + j.ToString();
Panel1.Controls.Add(fu);
}
TextBox2.Text = j.ToString();
j++;
TextBox1.Text = j.ToString();
}
protected void btn_down_Click(object sender, EventArgs e)
{
FileUpload fu;
if (k == 0)
{
return;
}
k = Convert.ToInt32(TextBox2.Text) - 1;
TextBox2.Text = k.ToString();
Panel1.Controls.Clear();
for (int i = 0; i < Convert.ToInt32(TextBox2.Text); i++)
{
fu = new FileUpload();
fu.ID = "fu" + i.ToString();
Panel1.Controls.Add(fu);
}
TextBox1.Text = (Convert.ToInt32(TextBox2.Text) + 1).ToString();
}
protected void btn_up_Click(object sender, EventArgs e)
{
string name, size, type
HttpFileCollection hfc = Request.Files;
//获取客户端文件集合
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
name = Path.GetFileName(hpf.FileName);
size = hpf.ContentLength.ToString();
type = hpf.ContentType;
hpf.SaveAs(Server.MapPath("upload") + "\\" + f_name);
}
}
}
客户端代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %>
<!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 id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Width="93px" Visible="False">1</asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Visible="False" /><br />
<br />
<asp:FileUpload ID="FileUpload1" runat="server" Width="228px" />
<asp:Panel ID="Panel1" runat="server" Height="50px" Width="125px">
</asp:Panel>
<br />
<asp:Button ID="btn_up" runat="server" OnClick="btn_up_Click" ToolTip="上传图片" Text="上传" />
<asp:Button ID="btn_down" runat="server" Width="21px" ToolTip="减少" OnClick="btn_down_Click" Text="-" Height="23px" />
<asp:Button ID="btn_add" runat="server" Width="24px" ToolTip="增加" OnClick="btn_add_Click" Text="+" />
</div>
</form>
</body>
</html>