JQuery按照form下载文件+后台asp.net,非ajax(下)

来源:互联网 发布:levis和lee知乎 编辑:程序博客网 时间:2024/05/22 00:35

    前面说到通过js form方式POST数据,那后台应该怎么处理了。

因为这代码的框架是一个外包写的,写的很复杂,可能也是之前所有的系统的合集。

暂时也没有时间去整理,所以只是在原有方式上增加功能。

作为一个C++的工程师,现在去搞前端。。。。

 

   后台有两种办法处理,一般的方式是先生成本地文件,然后在发送,还有就是直接发送数据。

我目前采用的是先生成本地文件。

看看代码吧

var filename = "withdraw_"+DateTime.Today.ToString("yyyyMMddhhmmss") + ".csv";            var path = "~/ApiViews/PXMServer/" + filename;            var filePathName = System.Web.HttpContext.Current.Server.MapPath(path);            ExportWithdraw(context, filePathName);            FileInfo fileInfo = new FileInfo(filePathName);            HttpContext.Current.Response.Clear();            HttpContext.Current.Response.ClearContent();            HttpContext.Current.Response.ClearHeaders();            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);            HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString());            HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");            HttpContext.Current.Response.ContentType = "application/octet-stream";            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");            HttpContext.Current.Response.WriteFile(fileInfo.FullName);            HttpContext.Current.Response.Flush();            HttpContext.Current.Response.End();


其他的函数可以忽略,最重要的是response的处理。

这样就完成了前后端的交互。

0 0