通过webservice jquery导出文件 [英] export file through webservice jquery

查看:66
本文介绍了通过webservice jquery导出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试使用Web方法和JQuery AJAX导出文件。文件下载的Web方法单独工作正常但是当我在AJAX中使用服务方法时它不工作

代码----



Hi,
I am trying to export file using Web Method and JQuery AJAX. Web method of file downloading individually working fine but when I am consuming service method in AJAX it is not working
code----

[WebMethod]
    public  String ExportToExcel()
    {
        try
        {
            // DataSet ds;
            HttpResponse response = HttpContext.Current.Response;
            // first let's clean up the response.object
            response.Clear();
            response.Charset = "";
            response.ContentEncoding = System.Text.Encoding.Default;

            // set the response mime type for excel
            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("Content-Disposition", "attachment;filename=\"ExportTest.xls\"");

            // create a string writer
            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {
                    response.Write(sw.ToString());
                    response.End();
                }
            }
            return "Exported";
        }
        catch (Exception ex) {
            return ex.Message;
        }
    }





Javascript调用:



Javascript calling:

$.ajax({
              url:"WebService.asmx?op=ExportToExcel"
          , type: "GET"
          ,contentType: "application/vnd.ms-excel"
          , data: {}
          , success: function (res) {
              alert("Success");
          }
          , error: function (res) {
              alert("Error");
          }
      });

推荐答案

.ajax({
url: WebService.asmx?op = ExportToExcel
,输入: GET
,contentType: application / vnd.ms-excel
,data:{}
,成功: function(res){
alert( Success);
}
,错误:函数(res){
alert( 错误 );
}
});
.ajax({ url:"WebService.asmx?op=ExportToExcel" , type: "GET" ,contentType: "application/vnd.ms-excel" , data: {} , success: function (res) { alert("Success"); } , error: function (res) { alert("Error"); } });


这篇关于通过webservice jquery导出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆