文章来源:http://www.2cto.com/kf/201205/132249.html


\

\

首先,将以下js代码放入页面这里我判断的是只能上传xls格式的文件,可以根据自己的需求修改js中的checkExcel方法


</pre><pre name="code" class="javascript"><script type="text/javascript"> 
    var __FILE_INDEX = 0; //文件标识    
    var __LOADING_TIP_DIV = null; 
    var __ICON_PATH = "<%=Request.ApplicationPath%>/Images"; 
    /**  
    * 对选择的文件进行格式校验,只能选择xls格式的文件  
    */ 
    function checkFile(fileObj) { 
        var objSpan = document.getElementById("span_" + fileObj.id); 
        if (!checkExcel(fileObj.value)) { 
            objSpan.innerHTML = "<img title='错误' src='" + __ICON_PATH + "/check_error.png' border='0'></img><font style='color:red;font-size:12px'>只能导入xls格式文件!</font>" 
            fileObj.errFlag = true; 
        } else { 
            objSpan.innerHTML = "<img title='正确' src='" + __ICON_PATH + "/check_right.png' border='0'></img>" 
            fileObj.errFlag = false; 
        } 
        if (fileObj.value != "" && fileObj.noDelete != "true") { 
            document.getElementById("del_" + fileObj.id).innerHTML = "<span title='删除文件' οnclick='deleteFile(\"" + fileObj.id + "\")' style='font-size:12px;color: #4684b2;cursor:pointer;border-bottom:1px solid #4684b2'>删除</span>"; 
        } 
    } 
 
    /**  
    * 删除选择的文件 www.2cto.com   
    */ 
    function deleteFile(fileId) { 
        var trNode = document.getElementById("tr_" + fileId); 
        var trParent = trNode.parentNode; 
        trParent.removeChild(trNode); 
    } 
 
    /**  
    * excel校验函数  
    */ 
    function checkExcel(filePath) { 
        var subfix = filePath.substring(filePath.lastIndexOf(".") + 1); 
        if (subfix != "xls") { 
            return false; 
        } 
        return true; 
    } 
    /**  
    * 文件上传时的校验逻辑  
    */ 
    function excelUpload() { 
        var fileCount = 0; 
        var files = document.getElementsByTagName("INPUT"); 
        for (var i = 0; i < files.length; i++) { 
            if (files[i].type.toLowerCase != 'file') continue; 
            if (files[i].errFlag) { 
                alert("导入的文件只能是xls格式,请重新选择."); 
                files[i].focus(); 
                return; 
            } 
            if (files[i].value != "") 
                fileCount++; 
        } 
        if (fileCount < 1) { 
            alert("请先选择要上传的数据文件!"); 
            return; 
        } 
        document.getElementById("uploadExcelForm").submit(); 
    } 
    /**  
    * 添加文件  
    */ 
    function addFile() { 
        ++__FILE_INDEX; 
        var fileId = "file" + __FILE_INDEX; 
        var uploadTable = document.getElementById("tableUploadFile") 
        var trElement = uploadTable.insertRow(-1); 
        trElement.id = "tr_" + fileId; 
        var tdElement = trElement.insertCell(-1); 
        tdElement.id = "td_" + fileId; 
        tdElement.innerHTML = "<input type='file' name='" + fileId + "' id='" + fileId + "' size='30' style='padding-left:5px;width:300px;cursor:pointer;' >" 
               + "<span id='span_" + fileId + "'></span>" 
               + "<span id='del_" + fileId + "'></span>"; 
    }    
</script> 

 


接下来是后台代码:


[csharp]  //保存上传文件 
        try 
        { 
            for (int i = 0; i < Request.Files.Count; i++) 
            { 
                if (Request.Files["file" + i] != null) 
                { 
                    HttpPostedFile filePost = Request.Files["file" + i]; 
                    string filename = filePost.FileName; 
                    Random rd = new Random(); 
                    filename = filename.Substring(filename.LastIndexOf('\\') + 1); 
                    string savePath = Server.MapPath("UpLoadFile/" + rd.Next() + filename); 
                    filePost.SaveAs(savePath); 
                    UpLoadFile upLoadfile = new UpLoadFile(); 
                    upLoadfile.FileObj = mubiao.Obj_Id; 
                    upLoadfile.FileAddr = savePath; 
                    upLoadfile.FileUpLoadDate = DateTime.Now; 
                    new UpLoadFileBLL().InsertUpLoadFile(upLoadfile); 
                } 
            } 
        } 
        catch (Exception) 
        { 
            PageMessageBoxShow("文件上传失败!");             
        } 



本文转载:CSDN博客