var myParent = parent.parent.parent.parent.parent.parent.parent.parent.parent.parent.parent.parent;FileDown = function (fileName, realFileName) { ???$(myParent.document.body).find("#down-file-iframe").remove(); ???var $iframe = $(‘<iframe name="_down-file-iframe" id="down-file-iframe" style="position:absolute;top:-10000px;left:-10000px;"></iframe>‘); ???var $a = $(‘<a href="‘ + fileName + ‘">下载</a>‘); ???if (realFileName != null) ???{ $a.attr("download", realFileName); } ???$(myParent.document.body).append($iframe); ???$iframe[0].onload = $iframe[0].onreadystatechange = function () { ???????var titleElem = $(this.contentWindow.document).find("title"); ???????if (titleElem.length > 0) { ???????????if (titleElem.text().indexOf("404") >= 0) ???????????{ myDialog({ title: "提示", content: "文件下载失败:<br/>" + "该文件不存在或已被删除" }); } ???????????else ???????????{ myDialog({ title: "提示", content: "文件下载失败:<br/>" + titleElem.text() }); } ?????????????????} ???} ???var DataExport_Timer = setInterval(function () { ???????var iframeObj = myParent.window.frames["down-file-iframe"].contentWindow || myParent.window.frames["down-file-iframe"]; ???????if ($(iframeObj.document.body).length <= 0) { return; } ???????clearInterval(DataExport_Timer); ???????$(iframeObj.document.body).append($a); ???????$a[0].click(); ???????setTimeout(function () { $iframe.remove(); }, 1000 * 120); ???}, 200);}
如果是弹出框形式的话,把此方法放在父窗体的页面,实现下载文件
调用
//遍历下载多个文件 ???????????for (var i = 0; i <= ids.length; i++) { ???????????????$.ajax({ ???????????????????url: ‘/Apply/DownloadScanFile‘, ???????????????????cache: false, ???????????????????async: false, ???????????????????type: ‘post‘, ???????????????????data: { id: ids[i] }, ???????????????????success: function (data) { ???????????????????????//CloseDialog(); ???????????????????????if (data.Status == 1) { ???????????????????????????FileDown(data.FullFileName, data.FileName); ???????????????????????} else { ???????????????????????????myDialog({ title: "提示", content: data.Error }); ???????????????????????}; ???????????????????} ???????????????}); ???????????}
控制器
/// <summary> ???????/// 下载回复文件 ???????/// </summary> ???????/// <param name="ids"></param> ???????/// <returns></returns> ???????public JsonResult DownloadFile(int id) ???????{ ???????????var model = Db.GetApplyResultFile(id); ???????????return Json(new { Status = 1, FullFileName = "\\" + model.FilePath, FileName = model.FileName }); ???????}
mvc 在弹出框中实现文件下载
原文地址:https://www.cnblogs.com/wlzhang/p/9988288.html