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

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

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

前台代码如下:

 


    <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>

 

后台获取文件代码:

 

 (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;
                }
            }

 

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

前台代码:

 


            <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();" />

后台获取:

 

//直接取所有文件            
HttpFileCollection hfCollection = Request.Files;

 

 

相关文章: