这里是修改的时候
前台
< script language ="JavaScript" >
var i = 0 ;
var n =<%= _n %> ;
function addFileControl()
{
if (i < ( 9 - n))
{
var str = ' <INPUT type="file" NAME="File"><br/> '
document.getElementById( ' FileCollection ' ).insertAdjacentHTML( " beforeEnd " ,str)
i ++ ;
} else
{
alert( ' 最多只能上传10个,还剩 ' + ( 10 - n) + ' 个可上传! ' )
}
}
</ script >
以下是body内
< table border ="0" cellpadding ="0" cellspacing ="0" width ="100%" >
< tr >
< td align ="center" style ="width: 227px; height: 25px" >
上传新图片
</ td >
< td style ="height: 25px" width ="72%" >
< input onclick ="addFileControl()" type ="button" value ="增加上传" />
< div id ="FileCollection" >
< input id ="File1" runat ="server" name ="File" type ="file" />< br />
</ div >
</ td >
</ tr >
< tr >
< td align ="center" style ="width: 227px; height: 25px" >
图片说明
</ td >
< td style ="height: 25px" >
< asp:TextBox ID ="txtTPSM" runat ="server" MaxLength ="25" ></ asp:TextBox >
</ td >
</ tr >
< tr style ="color: #000000" >
< td height ="25" style ="width: 227px" >
</ td >
< td height ="25" >
< asp:Button ID ="btnAdd" runat ="server" Text ="上传" OnClick ="btnAdd_Click" />
</ td >
</ tr >
</ table >
后台代码
定义 public int _n;-----n从数据读取已经添加了几条数据,如果是添加页面就不用n参数,去掉直接用数字
上传代码主要代码
Code
protected void btnAdd_Click(object sender, EventArgs e)
{
int fileCount = 0;
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
for (fileCount = 0; fileCount < files.Count; fileCount++)
{
//定义访问客户端上传文件的对象
System.Web.HttpPostedFile postedFile = files[fileCount];
string fileName, fileExtension;
//取得上传得文件名
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != String.Empty)
{
//取得文件的扩展名
fileExtension = System.IO.Path.GetExtension(fileName);
string guid_file = Guid.NewGuid().ToString();
string _filename = guid_file + fileExtension;
postedFile.SaveAs(Server.MapPath("../UploadFiles/") + _filename);
//此处添加到数据库操作
}
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
int fileCount = 0;
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
for (fileCount = 0; fileCount < files.Count; fileCount++)
{
//定义访问客户端上传文件的对象
System.Web.HttpPostedFile postedFile = files[fileCount];
string fileName, fileExtension;
//取得上传得文件名
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != String.Empty)
{
//取得文件的扩展名
fileExtension = System.IO.Path.GetExtension(fileName);
string guid_file = Guid.NewGuid().ToString();
string _filename = guid_file + fileExtension;
postedFile.SaveAs(Server.MapPath("../UploadFiles/") + _filename);
//此处添加到数据库操作
}
}
}