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;
    }
    }

  • 相关阅读:
    软件工程课程总结
    构建之法阅读笔记06
    个人工作总结11(第二阶段)
    第十六周学习进度
    大道至简阅读笔记03
    大道至简阅读笔记02
    Hadoop学习笔记(四):Yarn和MapReduce
    Hadoop学习笔记(三):java操作Hadoop
    Hadoop学习笔记(二):简单操作
    Hadoop学习笔记(一):安装与配置
  • 原文地址:https://www.cnblogs.com/c-x-a/p/7017823.html
Copyright © 2011-2022 走看看