zoukankan      html  css  js  c++  java
  • MVC js动态生成from提交数据然后生成文件下载

    前台: 点击触发下面事件

    var turnForm = document.createElement("form");
    //一定要加入到body中!!
    document.body.appendChild(turnForm);
    turnForm.method = 'post';
    turnForm.action = 'GetCSV';
    turnForm.target = '_blank';
    //创建隐藏表单
    var newElement = document.createElement("input");
    newElement.setAttribute("name", "csvFrom");
    newElement.setAttribute("type", "hidden");
    newElement.setAttribute("value", JSON.stringify(saveSiteArray));
    turnForm.appendChild(newElement);

    turnForm.submit();

    后台接收:

    public ActionResult GetCSV(FormCollection form)
    {
    try
    {
    List<AspSiteList> urlList = JsonToObj(form["csvFrom"], typeof(List<AspSiteList>)) as List<AspSiteList>;
    List<string> newUrlList = new List<string>();


    foreach (var item in urlList)
    {
    string newUrl = item.ClientId + " " + item.UneiUserName + " " + item.AsId + " " + item.AsNm + " " + item.Url;
    newUrlList.Add(newUrl);
    }
    string newString = string.Join(" ", newUrlList);

     Encoding encoder = Encoding.UTF8;

    byte[] bytes = encoder.GetBytes(newString);

    Response.Charset = "UTF-8";

    Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
    Response.ContentType = "application/octet-stream";

    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode("demo.csv"));
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
    return new EmptyResult();
    }
    catch (Exception)
    {

    throw;
    }
    }

  • 相关阅读:
    C#中跨线程访问控件问题解决方案
    asp.net网站中配置文件的加密
    C#中XML使用总结
    连接加密Access数据库的字符串设置方法
    asp.net中常用的26个优化性能的方法
    C#中Math的使用总结
    关于ASP.NET页面打印技术的总结
    域登录获取用户名字的控制
    Web界面设计基本原则
    域登录获得用户名称
  • 原文地址:https://www.cnblogs.com/c-x-a/p/7017823.html
Copyright © 2011-2022 走看看