多文件上传示例源码(默认支持各种类型,包括图片)

 
前几天在园子里发布了一个图片上传(加水印、缩略图、远程保存)的简单例子 ,很受大家的欢迎,这类再发布一个多文件上传示例源码(默认支持各种类型,包括图片)的例子,可以同时批量上传文件(包括图片)

网上包括园子里有很多多文件上传的例子,但是没有完整的示例代码,本人整理了一下发布出来,这样更直观,希望对大家有所帮助

开发环境VS2005

下载该源码

部分源码

None.gif   public  partial  class  _Default : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
static public ArrayList hif = new ArrayList(); // 保存文件列表
InBlock.gif
        public int filesUploaded = 0// 上传文件的数量
InBlock.gif

InBlock.gif        
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif        
/// 将要上传的文件添加到listbox中
InBlock.gif       
/// </summary>
InBlock.gif       
/// <param name="sender"></param>
ExpandedSubBlockEnd.gif       
/// <param name="e"></param>

InBlock.gif
InBlock.gif        
protected void AddFile_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (Page.IsPostBack == true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                hif.Add(FindFile);
InBlock.gif                FileList.Items.Add(FindFile.PostedFile.FileName);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{ }
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从listbox中删除指定的文件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sender"></param>
ExpandedSubBlockEnd.gif        
/// <param name="e"></param>

InBlock.gif        protected void RemvFile_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (FileList.SelectedIndex == -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                TipInfo.Text 
= "错误 - 必须指定要删除的文件.";
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (FileList.Items.Count != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                hif.RemoveAt(FileList.SelectedIndex);
InBlock.gif                FileList.Items.Remove(FileList.SelectedItem.Text);
InBlock.gif                TipInfo.Text 
= "";
ExpandedSubBlockEnd.gif            }
           
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif          
/**//// <summary>
InBlock.gif  
/// 循环上传listbox中的文件到指定的文件夹下
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="sender"></param>
ExpandedSubBlockEnd.gif  
/// <param name="e"></param>

InBlock.gif        public void Upload_ServerClick(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string baseLocation = Server.MapPath("UploadFiles/"); // 上传路径   
InBlock.gif
            string status = "";  // 上传成功后显示的文件列表         
InBlock.gif

InBlock.gif            
if ((FileList.Items.Count == 0&& (filesUploaded == 0))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                TipInfo.Text 
= "错误 - 必须指定要上传的文件.";
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
foreach (System.Web.UI.HtmlControls.HtmlInputFile HIF in hif)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
string fn = System.IO.Path.GetFileName(HIF.PostedFile.FileName);
InBlock.gif                        HIF.PostedFile.SaveAs(baseLocation 
+ fn);
InBlock.gif                        filesUploaded
++;
InBlock.gif                        status 
+= fn + "<br>";
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch (Exception err)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        TipInfo.Text 
= "上传错误 " + baseLocation
InBlock.gif                         
+ "<br>" + err.ToString();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
if (filesUploaded == hif.Count)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    TipInfo.Text 
= "共上传了 " + filesUploaded + " 个文件。 <br>" + status;
ExpandedSubBlockEnd.gif                }

InBlock.gif                hif.Clear();
InBlock.gif                FileList.Items.Clear();
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }




 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值