jsp页面多文件上传(转)

来源:互联网 发布:淘宝的面料跟材质 编辑:程序博客网 时间:2024/05/18 22:55

<html> 
<head id="Head1" runat="server"> 
    <title>无标题页 </title> 

    <script type="text/javascript"> 
function MultiSelector(list_target, max) 

    // Where to write the list 
    this.list_target = list_target; 
    // How many elements? 
    this.count = 0; 
    // How many elements? 
    this.id = 0; 
    // Is there a maximum? 
    if (max){this.max = max;}else{this.max = -1;} 
    /** 
    * Add a new file input element 
    */ 
    this.addElement = function(element) 
    { 
        // Make sure it's a file input element 
        if (element.tagName == 'INPUT' && element.type == 'file') 
        { 
            // Element name -- what number am I? 
            element.name = 'file_' + this.id++; 
            // Add reference to this object 
            element.multi_selector = this; 
            // What to do when a file is selected 
            element.onchange = function() 
            { 
                var inputs = document.getElementsByTagName("input"); 
                var count = 0; 
                for(var i=0;i <inputs.length;i++) 
                { 
                    if (inputs[i].type == 'file') 
                    { 
                        if(this.value==inputs[i].value) 
                        { 
                            count++; 
                        } 
                    } 
                }            
                if(count>1) 
                { 
                    alert("该文件已存在!"); 
                    //this.outerHTML=this.outerHTML; 
                    return false; 
                } 
                if(!this.value.IsPicture()) 
                { 
                    alert("只能上传图片!"); 
                    //this.outerHTML=this.outerHTML; 
                    return false; 
                } 
                
                // New file input 
                var new_element = document.createElement('input'); 
                new_element.type = 'file'; 
                new_element.size = 1; 
                new_element.className = "addfile"; 
                // Add new element 
                this.parentNode.insertBefore(new_element, this); 
                // Apply 'update' to element 
                this.multi_selector.addElement(new_element); 
                // Update list 
                this.multi_selector.addListRow(this); 
                // Hide this: we can't use display:none because Safari doesn't like it 
                this.style.position = 'absolute'; 
                this.style.left = '-1000px'; 
            } 
            // If we've reached maximum number, disable input element 
            if (this.max != -1 && this.count >= this.max) 
            { 
                element.disabled = true; 
            } 
            // File element counter 
            this.count++; 
            // Most recent element 
            this.current_element = element; 
        } 
        else 
        { 
            // This can only be applied to file input elements! 
            alert('Error: not a file input element'); 
        } 
    } 
    /** 
    * Add a new row to the list of files 
    */ 
    this.addListRow = function(element) 
    {    
        // Row div 
        var new_row = document.createElement('div'); 
        new_row.className="divName"; 
        // Delete button 
        var new_row_button = document.createElement('input'); 
        new_row_button.type = 'button'; 
        new_row_button.value = '删除'; 
        // References 
        new_row.element = element; 
        // Delete function 
        new_row_button.onclick = function() 
        { 
            // Remove element from form 
            this.parentNode.element.parentNode.removeChild(this.parentNode.element); 
            // Remove this row from the list 
            this.parentNode.parentNode.removeChild(this.parentNode); 
            // Decrement counter 
            this.parentNode.element.multi_selector.count--; 
            // Re-enable input element (if it's disabled) 
            this.parentNode.element.multi_selector.current_element.disabled = false; 
            // Appease Safari 
            // without it Safari wants to reload the browser window 
            // which nixes your already queued uploads 
            return false; 
        } 
        // Set row value 
        var filename = element.value.substring(element.value.lastIndexOf("//")+1); 
        new_row.innerHTML = filename; 
        // Add button 
        new_row.appendChild(new_row_button); 
        // Add it to the list 
        this.list_target.appendChild(new_row); 
    } 

String.prototype.IsPicture = function() 
    { 
        //判断是否是图片 - strFilter必须是小写列举 

        var strFilter=".jpeg|.gif|.jpg|.png|.bmp|.pic|" 
        if(this.indexOf(".")>-1) 
        { 
            var p = this.lastIndexOf("."); 
            var strPostfix=this.substring(p,this.length) + '|';        
            strPostfix = strPostfix.toLowerCase(); 
            if(strFilter.indexOf(strPostfix)>-1) 
            { 
                return true; 
            } 
        }        
        return false;            
    } 
    </script> 

    <style type="text/css"> 
    a.addfile 
    { 
    background-image: url(f1.gif); 
    background-repeat: no-repeat; 
    background-position: -823px -17px; 
    display: block; 
    float: left; 
    height: 25px; 
    margin-top: -1px; 
    position: relative; 
    text-decoration: none; 
    top: 0pt; 
    width: 65px; 
    overflow:hidden; 
    } 
    a.addfile:hover 
    { 
    background-position: -911px -17px; 
    } 

    input.addfile 
    { 
    cursor: pointer !important; 
    height: 25px; 
    left: -6px; 
    filter: alpha(opacity=0); 
    position: absolute; 
    width: 0px; 
    z-index: -1; 
    } 
    .divName 
    { 
        float:left; 
        margin-left:5px; 
    } 
    </style> 
    <base target="_self" /> 
</head> 
<body> 
    <form id="form1" runat="server" enctype="multipart/form-data"> 
        <div> 
            <table width="100%" style="text-align: left;"> 
                <tr> 
                    <th align="center"> 
                        添加文件 
                    </th> 
                </tr> 
                <tr> 
                    <td> 
                        <a href="#" class="addfile" id="al"> 
                            <input id="my_file_element" class="addfile" runat="server" type="file" name="file_1" size="1" title="点击选择附件" /> 
                        </a>&nbsp; 
                        <asp:Button ID="btn_Ok" Height="25px" runat="server" Text="确定" OnClick="btn_Ok_Click" /> 
                        <br /> 
                        <%--Files:--%> 
                        <!-- This is where the output will appear --> 
                        <div id="files_list" style="border: none;width: 100%;"> 
                        </div> 
                        <script type="text/javascript"> 
                        <!-- Create an instance of the multiSelector class, pass it the output target and the max number of files --> 
                        var multi_selector = new MultiSelector(document.getElementById('files_list'), 100); 
                        <!-- Pass in the file element --> 
                        multi_selector.addElement(document.getElementById('my_file_element')); 
                        </script> 
                    </td> 
                </tr> 
            </table> 
        </div> 
    </form> 
</body> 
</html>