本文章主要用於記錄,方便以后的翻找使用!!!!
html:
1 <tr> 2 <th class='formTitle'>選擇文件: 3 <input type="file" id="file_input" name="file_input" style="left: -9999px; position: absolute;" multiple="multiple" /> 4 </th> 5 <td class="formValue" colspan="4" > 6 <div style="text-align: center; width: 100%" id="uploadFile"> 7 <input type="button" onclick="addfileclick();" id="uploadPic" value="上 傳" style="width: 80%" /></div> 8 </td> 9 </tr> 10 //以下是加載當前數據已經存在的文件的記錄,包括預覽,下載,刪除;XXXXX 后台查詢的類 11 @if (ViewData["files"] != null) 12 { 13 int i = 1; 14 foreach (var file in ViewData["files"] as IEnumerable<XXXXXX>) 15 { 16 <tr id="oldFile_@file.Id"> 17 <th class="formTitle">已傳文件:</th> 18 <td class="formValue" colspan="2"> 19 <label id="fileName">@file.FileName</label> 20 </td> 21 <td class="formValue" colspan="2"> 22 <div style="text-align: center;"> 23 @*<a href="../@file.FilePath" id="preFile" target="_blank">預覽</a> *@ 24 <a href="javascript:;" id="preFile" title="/@file.FilePath" name="@file.FileName" onclick="preFile(this.title,this.name,'@file.Ext')">預覽</a> 25 <a href="javascript:;" id="downFile" title="../@file.FilePath" name="@file.FileName" onclick="downFile(this.title,this.name)">下載</a> 26 <a href="javascript:;" id="@file.Id" onclick="deleteFile(this.id)">刪除</a> 27 </div> 28 </td> 29 </tr> 30 i++; 31 } 32 }
頁面加載時,為input添加change事件:
$(function () { var input = document.getElementById("file_input"); if (typeof FileReader === 'undefined') { alert("抱歉,你的瀏覽器不支持 FileReader"); input.setAttribute('disabled', 'disabled'); } else { input.addEventListener('change', uploadFile, false); } });
后台需要獲取文件,進行頁面文件的初始加載:
public ActionResult Form(string KeyValue) { if (!string.IsNullOrEmpty(KeyValue))//編輯 { var files = GetFiles(KeyValue); if (files.Count > 0) { ViewData["files"] = files; } } return View(); } public List<XXXXXX> GetFiles(string Id) { return OperateContext.Current.BllContext.IXXXXXXBll.GetList(c => c.Id== Id && c.XX== "XX").ToList(); }
然后就是頁面上的js來處理了:
1 //點擊上傳按鈕進行文件的選擇 2 function addfileclick() { 3 document.getElementById("file_input").click(); 4 } 5 var num = 0;//最終上傳的文件的數量 6 var index = 0;//選擇的文件的"索引" 7 var deleteIndex = "";//js中也就是前端進行並未上傳到后台的文件的刪除 8 9 function uploadFile() { 10 num = 0; 11 index = 0; 12 deleteIndex = ""; 13 $(".newFileClass").remove(); 14 var iLen = this.files.length;//選擇的文件的數量 15 for (var i = 0; i < iLen; i++) {//循環選擇文件進行展示,並添加刪除按鈕,可以進行刪除 16 var html = "<tr id='newFile_" + index + "' class='newFileClass'><th class='formTitle'>文件名:</th><td class='formValue' colspan='2'><label id='fileName'>" + this.files[i].name + "</label></td><td class='formValue' colspan='2'><div style='text-align:center;'><a href='javascript:;' id='deleteFile' onclick = 'deleteFile(" + index + ");'>刪除</a></div></td></tr>"; 17 $("#FileTable").append(html); 18 index++; 19 num++; 20 } 21 22 } 23 //刪除文件--后台刪除文件,已經在后台保存過的文件的刪除 24 function deleteFile(index) { 25 if (!!GetQuery('KeyValue') && !checkRate(index)) {//編輯 26 if (!confirm("注:您確定要刪除上傳記錄?")) 27 { return false; } 28 var fileId = index; 29 AjaxJson(getControlStr() + "/User_DiseaseDiagnose/DeleteFile", { KeyValue: GetQuery('KeyValue'), fileId: fileId }, function (Data) { 30 layer.msg(Data.Message, { 31 icon: Data.State, 32 time: 1500 33 }, function () { 34 if (Data.State == 1) { 35 //window.location.reload(); 36 } 37 else { 38 $("#oldFile_" + index).remove(); 39 } 40 layer.closeAll(); 41 }); 42 }); 43 } 44 else {//新增 45 num--; 46 //fd.delete(index); 47 $("#newFile_" + index).remove(); 48 deleteIndex += (index + ","); 49 } 50 } 51 //正則表達式 驗證數據 52 function checkRate(value) { 53 var re = /^[0-9]+[0-9]*]*$/; //判斷正整數 /^[1-9]+[0-9]*]*$/ 54 if (!re.test(value)) { 55 return false; 56 } else { 57 return true; 58 } 59 } 60 //預覽文件,其中FleView為一個頁面,用來顯示文件內容,FilePath為文件的保存路徑;ext用來區分不同文件類型,按照不同方式顯示 61 function preFile(path, name, ext) { 62 parentDialogWithClose(getControlStr() + "/ControllerName/FileView?FilePath=" + escape(path) + "&ext=" + ext, '', name, "100%", "100%"); 63 } 64 //下載文件,通過創建a標簽實現點擊下載 65 function downFile(path, name) { 66 var alink = document.createElement("a"); 67 alink.href = path; 68 alink.download = name; 69 alink.click(); 70 } 71 //layer.open打開彈窗,在彈窗中顯示文件。其中parent.parent.可以根絕實際情況修改 72 function parentDialogWithClose(url, _id, _title, _width, _height) { 73 if (_width != "100%") { 74 _width = _width + "px"; 75 _height = _height + "px"; 76 } 77 parent.parent.layer.open({ 78 type: 2, 79 title: _title, 80 shadeClose: true, 81 shade: 0.01, 82 scrollbar: false, 83 maxmin: true, //開啟最大化最小化按鈕 84 area: [_width, _height], 85 content: url 86 }); 87 }
其中的FileView頁面內容:本頁面主要用來區分word文檔與其他文件的展示。
@{ ViewBag.Title = "FileView"; Layout = "~/Views/Shared/_Layout.cshtml"; } <script type="text/javascript"> $(function () { var FilePath = (GetQuery('FilePath')); var ext = GetQuery("ext"); if (!!ext) { debugger; if (ext == '.doc' || ext == '.docx') { $("#myIframe").attr("src", "https://view.officeapps.live.com/op/view.aspx?src=" + window.location.host + getControlStr() + FilePath); } else if (ext == '.jpg' || ext == '.jpeg' || ext == '.png' || ext == '.bmp' || ext == '.gif') { var html = "<img src='" + getControlStr() + FilePath + "' style='margin:0 auto;min-width:400px;display:block;'>"; $("#myIframe").contents().find("body").append(html) } else { $("#myIframe").attr("src", getControlStr() + FilePath); } } }); </script> <body style="height:100%;"> <iframe id="myIframe" src='' frameborder='1' style="text-align:center; width:100%; height:100%;"></iframe> </body>
記錄下來,方便以后使用時查找。