批量上传功能

最近的项目要做批量上传功能,初次接触,绕了好多弯:

就应用来说,批量上传目前有两种方式,一种是添加一个文件上传一个文件,例如live mail;另外一种是多次添加,统一上传的方式,例如126邮箱。

我初次使用的是live mail的方式

前台代码如下:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
<asp:FileUpload ID="FileUpload" runat="server" Width="90%" Visible="false" />
<asp:Button ID="BtnAddUpload" runat="server" Text="添加附件" OnClick="BtnAddUpload_Click" />
<br />
<div id="divFiles" runat="server">
    
<asp:GridView ID="GVUpload" SkinID="OrdinaryGridView" runat="server" DataKeyNames="KnowledgeStoreId,KnowledgeAnnexID,AnnexName" Width="100%" OnRowDeleting="GVUpload_RowDeleting">
        
<Columns>
            
<asp:BoundField DataField="AnnexName" HeaderText="文件名" ItemStyle-Width="60%" />
            
<asp:TemplateField><ItemTemplate><href='<%#Eval("FilePath") %>' target="_blank"><%#Eval("AnnexName")%></a></ItemTemplate></asp:TemplateField>
            
<asp:CommandField ButtonType="Link" ShowDeleteButton="true" DeleteText="移除" />
        
</Columns>
    
</asp:GridView>
</div>

 

后台获取文件代码:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
if (FileUpload.HasFile)
            {
                
//建立文件夹
                string mainPath = indexer.SearchConfiguration.FilePath;
                mainPath 
= Server.MapPath(mainPath);
                
if (mainPath.LastIndexOf('\\'!= mainPath.Length - 1)
                {
                    mainPath 
+= "\\";
                }
                
string subPath = "";
                
if (BtnDel.Visible)
                {
                    subPath 
= mainPath + Request.Params["TypeId"].ToString().Trim();

                }
                
else
                {
                    
if (string.IsNullOrEmpty(HFFiles.Value))
                    {
                        HFFiles.Value 
= Guid.NewGuid().ToString();
                    }
                    subPath 
= mainPath + "temp_" + HFFiles.Value;
                }
                
if (!Directory.Exists(subPath))
                {
                    Directory.CreateDirectory(subPath);
                }
                
string fileName = FileUpload.FileName;
                
//保存文件
                if (!File.Exists(subPath + "\\" + fileName))
                {
                    FileUpload.PostedFile.SaveAs(subPath 
+ "\\" + fileName);
                }
                
else
                {
                    Alert(
"此文件已经存在,请修改文件名");
                    
return;
                }
            }

 

后来发现还是网易的方式比较方便,实现起来也简单

前台代码:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
        <id="FileList">
            
<input id="File1" type="file" runat="server" size="50" name="File"/></p>
        
<script language="javascript" type="text/javascript">   
        
function  addFile()   
                  {   
                          
var   filebutton   =   '<br/><input   type="file"   size="50"   name="File"   class="ButtonCss"   />';   
                          document.getElementById(
'FileList').insertAdjacentHTML("beforeEnd",filebutton);   
                  }   
        
</script>
        
<input id="BtnAdd" type="button" value="添加" onclick="addFile();" />

后台获取:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
//直接取所有文件            
HttpFileCollection hfCollection = Request.Files;

 

 

转载于:https://www.cnblogs.com/goodbye305/archive/2008/08/25/1275468.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值