ASP.NET MVC-异步发送post请求+文件下载

环境:

win10, .NET 6.0


前端向后台传递string型变量

前端:

javascript 复制代码
    function PasteSubmit() {
    	// 获取某个input的值
        var inName = document.getElementById("xx").value;
        // 获取某个元素的属性值
        var inSeq = document.getElementById("xxx").getAttribute("xxx");
        $.ajax({
            type: 'POST',
            url: "/Home/PasteSubmit",
            data: {
                inName: inName,
                inSeq: inSeq,
            },
            success: function (resp) {
            	console.log(resp);
            }
        });
    }

后台:(HomeController)

csharp 复制代码
[HttpPost]
public ActionResult PasteSubmit() {
    // 获取数据
    string name = Request.Form["inName"]; 
    string seq = Request.Form["inSeq"];
    System.Diagnostics.Trace.WriteLine(name);
    System.Diagnostics.Trace.WriteLine(seq);
    // do something
    // 返回的内容
    return Content("test");
}

前台向后台传递多个List

说明:先传递三个List,后台接收后做一些处理,然后回传字符串,将字符串写入文件,生成下载文件,自动下载。

前端:

html 复制代码
@{
// 定义
List<int> cstarts = new List<int>() { 1, 2, 3};
List<int> cends = new List<int>() { 4, 5, 6 };
List<string> cstrs = new List<string>() { "", "sss", "x"};
}

<!-- html内容略 -->

<button id="exportSeqBtn" onclick="exportSeq()" type="button">
    导出序列
</button>

<!-- js -->
<script>
    // 导出序列
    function exportSeq() {
        var starts = @Html.Raw(Json.Encode(cstarts));
        var ends = @Html.Raw(Json.Encode(cends));
        var strs = @Html.Raw(Json.Encode(cstrs));
        $.ajax({
            url: "/Home/ExportSeq",
            method: 'POST',
            data: JSON.stringify({
                vid: @Model.Id,
                cstarts: starts,
                cends: ends,
                cstrs: strs,
            }),
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                var blob = new Blob([data], { type: 'text/plain' });
                var link = document.createElement('a');
                link.href = window.URL.createObjectURL(blob);
                link.download = 'sequence.fa'; // 下载文件的文件名
                link.click();
            },
            error: function (xhr, status, error) {
                alert('An error occurred while generating the file: ' + error);
            }
        });
    }
</script>

后端:

csharp 复制代码
[HttpPost]
public ActionResult ExportSeq(string vid, List<int> cstarts, List<int> cends, List<string> cstrs) {
	var txtContent = new StringBuilder();
    // do something
    txtContent.AppendLine(string.Join(",", cstarts));
    // 可以打印其他的看看
    return Content(txtContent.ToString(), "text/plain");
}
相关推荐
程序员岳焱4 小时前
Java 与 MySQL 性能优化:Java 实现百万数据分批次插入的最佳实践
后端·mysql·性能优化
麦兜*5 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
大只鹅5 小时前
解决 Spring Boot 对 Elasticsearch 字段没有小驼峰映射的问题
spring boot·后端·elasticsearch
ai小鬼头5 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
前端·后端·github
IT_10246 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle
bobz9656 小时前
动态规划
后端
stark张宇6 小时前
VMware 虚拟机装 Linux Centos 7.9 保姆级教程(附资源包)
linux·后端
亚力山大抵7 小时前
实验六-使用PyMySQL数据存储的Flask登录系统-实验七-集成Flask-SocketIO的实时通信系统
后端·python·flask
超级小忍7 小时前
Spring Boot 中常用的工具类库及其使用示例(完整版)
spring boot·后端
CHENWENFEIc8 小时前
SpringBoot论坛系统安全测试实战报告
spring boot·后端·程序人生·spring·系统安全·安全测试