返回带有 JQuery 的动态文件和带有 POST 参数的 ASHX



嗨,我需要在服务器端生成一些文件并使用 AJAX 将它们返回到客户端

我在服务器 (ASHX) 上创建下一个代码

public void ProcessRequest(HttpContext context)
    {
        string dataViewID = context.Request.Form["dataViewID"];
        MyService service = new MyService();
        var data = service.GetStores(int.Parse(dataViewID), "", null);
        IMyExportService exportservice = new MyExportService();
        HttpContext.Current.Response.ContentType = "application/octet-stream";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + "export.cvs");  
        using (var ms = new MemoryStream())
        {
            using (var sw = new StreamWriter(ms))
            {
                exportservice.ExportTo("csv", sw, data);                
                ms.Position = 0;                                                
                HttpContext.Current.Response.Write(ms.ToArray());
            }
        }
    }

在客户端上,我创建下一个代码:$("#btnexport").click(function () { var paramData = { "dataViewID": 1524129, "filter": ", extent: null };完整地图 $.ajax({ url: '/marketVuePortal/'+'FileExport.ashx', 类型:"开机自检",
数据类型:"json", data: {dataViewID:1524129},
成功:函数(结果){ 这里应该是什么? }, 错误: 函数 (xhr) { 警报("错误"); }
} )}

);

但是我有 2 个我不知道为什么的问题,但我总是收到错误,但在调试中所有代码都运行良好。 其次,我不知道如何说浏览器,他需要保存页面而无需重新加载。

/* * -------------------------------------------------------------------- * jQuery-Plugin - $.download - 允许对文件进行简单的获取/发布请求 * 作者:Scott Jehl, scott@filamentgroup.com * http://www.filamentgroup.com * 参考文章: http://www.filamentgroup.com/lab/jquery_plugin_for_requesting_ajax_like_file_downloads/ * 版权所有 (c) 2008 Filament Group, Inc * 根据 MIT (filamentgroup.com/examples/mit-license.txt) 和 GPL 获得双重许可 (filamentgroup.com/examples/gpl-license.txt) 许可证。 * -------------------------------------------------------------------- */jQuery.download = function(url, data, method){//url and data options required if( url && data ){//data 可以是字符串 参数或数组/对象数据 = 数据类型 == '字符串' ?数据: jQuery.param(data); 将参数拆分为表单输入 var 输入 = ''; jQuery.each(data.split('&'), function(){ var pair = this.split('='); 输入+=''; }); 发送请求 jQuery(''+inputs+'') .appendTo('body').submit().remove(); };};

这是我找到的解决方案。

最新更新