多文件(图片)上传-限制上传个数

这里是修改的时候

前台


< 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参数,去掉直接用数字

上传代码主要代码


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);
                
//此处添加到数据库操作

            }
        }
        
      

    }
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值