在点击增加图片的时候会自动增加上传图片的数量默认是上传一张,其上传数量的多少由开发者自己定义
下面是全部代码:
前台布局代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 type="text/javascript">
var i=1
function addFile()
{
if (i<8)
{var str = '<BR> <input type="file" name="File" runat="server" style="width: 300px"/>'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
}
else
{
alert("您一次最多只能上传8张图片!")
}
i++
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="Table1" align="center" border="0" cellpadding="1" cellspacing="1" >
<tr>
<td align="center">
<font color="#0000ff" face="宋体" size="3"><strong>上传图片</strong></font></td>
</tr>
<tr>
<td align="center">
</td>
</tr>
<tr>
<td align="center">
<asp:Panel ID="Panel5" runat="server">
<table>
<tr>
<td align="right">
</td>
<td align="left">
说明:点增加图片按钮可一次上传多张图片。单张图片大小不大于1024k
</td>
</tr>
<tr>
<td align="right" >
请选择图片:<br />
</td>
<td align="left">
<p id="MyFile"><input οnclick="addFile()" type="button" value="增加图片(Add)"><br />
<input id="File1" runat="server" name="File" style="width: 300px" type="file" /></p>
</td>
</tr>
<tr>
<td align="right">
</td>
<td align="left">
<asp:Button ID="btnUpload" runat="server" Text="开始上传" οnclick="btnUpload_Click" />
</td>
</tr>
<tr>
<td align="right">
</td>
<td align="left">
<asp:Label ID="lblMessage" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>
</td>
</tr>
</table>
</asp:Panel>
</td>
</tr>
</table>
<asp:Button ID="Button1" runat="server" Text="显示所有图片" οnclick="Button1_Click1" />
<div runat=server id="div1">
</div>
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpload_Click(object sender, EventArgs e)
{
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;//提供客户端上传文件的访问
System.Text.StringBuilder strmsg = new System.Text.StringBuilder("");
Response.Write(files.Count);
string path = Server.MapPath("~/Pic/");//上传路劲
for (int i = 0; i < files.Count; i++)
{
if (files[i].FileName.Length > 0)
{
System.Web.HttpPostedFile postFile = files[i];
if (postFile.ContentLength > 1048576)
{
strmsg.Append(Path.GetFileName(postFile.FileName) + "不能大于1兆<br>");
return;
}
string fex = Path.GetExtension(postFile.FileName);//取得上传文件的扩展名
if (fex != ".jpg" && fex != ".JPG" && fex != ".gif" && fex != ".GIF")
{
strmsg.Append(Path.GetFileName(postFile.FileName) + "上传格式不正确<br>");
return;
}
}
}
if (strmsg.Length <= 0)//一切正常可以上传
{
for (int i = 0; i < files.Count; i++)
{
if (files[i].FileName.Length <= 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "alert('请选择上传文件')", true);
return;
}
else
{
System.Web.HttpPostedFile postFile = files[i];
string imageName = postFile.FileName;
int count = imageName.LastIndexOf(@"/") + 1;//返回这个字符在这个字符串中最后出现的位置
string image = imageName.Substring(count);
files[i].SaveAs(path + image);
}
}
Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "alert('上传成功')", true);
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
div1.InnerHtml = Show();
}
/// <summary>
/// 显示所有图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private string Show()
{
string[] ss = Directory.GetFiles(Server.MapPath("~/Pic"));
string path = "";
System.Text.StringBuilder bu = new System.Text.StringBuilder("");
for (int i = 0; i < ss.Length; i++)
{
string str = ss[i];
string image = str.Substring(str.LastIndexOf(@"/"));
//Response.Write("<img alt='" + image + "' src='" + str + "' style='width: 98px' /> ");
bu.Append("<img alt='" + image + "' src='" + str + "' style='width: 98px' /> ");
}
path = bu.ToString();
return path;
}
}