ASP.net MVC 如何打开新标签页下载文件?

疯快者 2019-12-03 05:49:04
领导要求导出的Excel文件能让用户指定到想导出的文件夹上.
我想到的是改成下载,让客户自己另存为进行保存.

View代码如下


// 导出按钮
$("[name=FileOutputButton]").click(function () {

var form = $("form#FileOutput");
//略...
form.submit();
});


@using (Html.BeginForm("C05_01_FileOutput", "C05", FormMethod.Post, new { id = "FileOutput" }))
{
@Html.HiddenFor(model => model.YEARS);
@Html.HiddenFor(model => model.RECORDING_MONTH);
@Html.HiddenFor(model => model.InsertUpdateFlg);
}



控制台代码如下

public ActionResult C05_01_FileOutput(xxx form)
{

//检索数据
......

//保存地址
string tmpPath = Request.MapPath("~/template");
tmpPath = Path.Combine(tmpPath + "/C05_01.xlsx");

string tmpDLPath = Request.MapPath("~/Tmp");

//文件输出
FinancialDB.fileOutput(form, tmpPath,ref tmpDLPath);

//返回文件
return File(new FileStream(tmpDLPath, FileMode.Open), "application/octet-stream", Server.UrlEncode("C05_01.xlsx"));
}


这么做的缺点是当前页面要等它执行完产生下载的提示,
期间如果用户按了画面其它按钮就得不到文件了.

所以我想到,按导出按钮的时候 打开新的标签页 再执行文件导出处理,
可是查了好久都没能成功.求大神帮助!!!

...全文
62 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
疯快者 2019-12-03
  • 打赏
  • 举报
回复
好像自己解决了,不知道其它还有没有更好的方式 View修改如下

   $("[name=FileOutputButton]").click(function () {

            var form = $("form#FileOutput");
           
            var years = year[0].options[year[0].options.selectedIndex].value;
            var month=recordingMonth[0].options[recordingMonth[0].options.selectedIndex].value;
            
            window.open('/C05/C05_01_FileOutput?year='+years+"&month="+month);

        });

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧