<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
#txt{
display: none;
}
</style>
</head>
<body>
<div id="txt">
<pre id="jsonPre"></pre>
</div>
<a>点击</a>
</body>
</html>
<script type="text/javascript">
$(function(){
$.post("/api/getToTxt.ashx", function(data) {
var Data = JSON.parse(data).Data;
console.info(Data);
$('#jsonPre').text(parse(Data));
var time = new Date();
var year=time.getFullYear();
var month=(time.getMonth()+1)<10?("0"+(time.getMonth()+1)):(time.getMonth()+1);
var date=time.getDate()<10?("0"+time.getDate()):time.getDate();
var hours=time.getHours()<10?("0"+time.getHours()):time.getHours();
var min=time.getMinutes()<10?("0"+time.getMinutes()):time.getMinutes();
var sec=time.getSeconds()<10?("0"+time.getSeconds()):time.getSeconds();
var title=year+month+date+hours+min+sec;
$("a").attr("href", 'data:text/plain;charset=utf-8,' + encodeURIComponent($('#jsonPre').text()));
$("a").attr('download', title);
})
})
function parse(str) {
return JSON.stringify(str, null, "\t");
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace getDataToTxt
{
/// <summary>
/// getToTxt 的摘要说明
/// </summary>
public class getToTxt : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//解决跨域问题
context.Response.AddHeader("Access-Control-Allow-Origin", "*");
context.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
context.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type");
string ljzfc = System.Configuration.ConfigurationManager.AppSettings["SqlConnLZ"].ToString();
string sql = "select Top 600 ROW_NUMBER() OVER (ORDER BY ID DESC) 序号,CaseID 案件编码,CaseYear,CaseCode 案件编号,CaseFrom 案件来源,DriverAddr 案发地点,CaseTitle 案由,CaseTime 案发时间,LawItem_Punish 处罚依据,CaseMoney 处罚金额,LawUnit 执法单位 from DJB_CF order by ID desc";
var list = DBHelper.select(ljzfc,sql);
List<Data> datas = new List<Data>();
if (list != null)
{
for (int i = 0; i < list.Rows.Count; i++)
{
Data data = new Data();
data.IDS = int.Parse(list.Rows[i]["序号"].ToString());
data.CaseID = list.Rows[i]["案件编码"].ToString();
data.CaseCode ="仪路政罚 【"+ list.Rows[i]["CaseYear"].ToString()+"】 "+list.Rows[i]["案件编号"].ToString()+" 号";
data.CaseFrom = list.Rows[i]["案件来源"].ToString();
data.CaseTitle = list.Rows[i]["案由"].ToString();
data.CaseTime = list.Rows[i]["案发时间"].ToString();
data.DriverAddr = list.Rows[i]["案发地点"].ToString();
data.LawItem_Punish = list.Rows[i]["处罚依据"].ToString();
data.CaseMoney = list.Rows[i]["处罚金额"].ToString();
data.LawUnit = list.Rows[i]["执法单位"].ToString();
datas.Add(data);
}
}
HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { Result = 200, Msg = "获取数据成功", Data = datas.ToArray() })
.Replace("IDS","序号")
.Replace("CaseID","案件编码")
.Replace("CaseCode","案件编号")
.Replace("CaseFrom","案件来源")
.Replace("DriverAddr","案发地点")
.Replace("CaseTitle","案由")
.Replace("CaseTime","案发时间")
.Replace("LawItem_Punish","处罚依据")
.Replace("LawUnit","执法单位")
.Replace("CaseMoney","处罚金额"));
}
public bool IsReusable
{
get
{
return false;
}
}
}
}