asp.net多文件的上传、预览、下载
本文章主要用于记录,方便以后的翻找使用!!!!
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>
记录下来,方便以后使用时查找。
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步