【问题标题】:Downloading Excel File via webmethod response by ajax calling通过ajax调用通过webmethod响应下载Excel文件
【发布时间】:2015-08-13 11:44:43
【问题描述】:

我创建了一个 webmethod,该方法只是将 Excel 文件作为 webresponse 发送。当我只运行 webmethod 时,它可以正常工作

我的网络方法如下:

 public void Export_ex(string elem)
    {

            string elements = elem;
            HttpContext.Current.Response.ContentType = "data:application/vnd.ms-excel";
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=Print.xls");
            HttpContext.Current.Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
            HttpContext.Current.Response.Write("<head>");
            HttpContext.Current.Response.Write("<div>");
            HttpContext.Current.Response.Write("<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-     8\">");
            HttpContext.Current.Response.Write("<!--[if gte mso 9]><xml>");
            HttpContext.Current.Response.Write("<x:ExcelWorkbook>");
            HttpContext.Current.Response.Write("<x:ExcelWorksheets>");
            HttpContext.Current.Response.Write("<x:ExcelWorksheet>");
            HttpContext.Current.Response.Write("<x:Name>Report Data</x:Name>");
            HttpContext.Current.Response.Write("<x:WorksheetOptions>");
            HttpContext.Current.Response.Write("<x:ValidPrinterInfo/>");
            HttpContext.Current.Response.Write("</x:Print>");
            HttpContext.Current.Response.Write("</x:WorksheetOptions>");
            HttpContext.Current.Response.Write("</x:ExcelWorksheet>");
            HttpContext.Current.Response.Write("</x:ExcelWorksheets>");
            HttpContext.Current.Response.Write("</x:ExcelWorkbook>");
            HttpContext.Current.Response.Write("</xml>");
            HttpContext.Current.Response.Write("<![endif]--> ");
            HttpContext.Current.Response.Write("</head>");
            HttpContext.Current.Response.Write(elements);

            HttpContext.Current.Response.Flush();
           // HttpContext.Current.Response.End();


        }

但是当我通过 ajax 调用从 javascript 函数调用它时,什么也没发生。我的 ajax 方法如下:

 var str = "something"
             var data = { elem: str };
             $.ajax({
                 type: 'POST',
                 url: "ImageSaving.asmx/Export_ex",
                 data: JSON.stringify(data),
                 contentType: "application/json; charset=utf-8",
                 mimeType: 'application/vnd.ms-excel',
                 success: function (response) {
                     var show = response.d;

                  },
                 failure: function (msg) {
                     alert("Error occur, could not load the service.");
                 }
             });

我不明白我哪里出错了??对此有何建议?

【问题讨论】:

    标签: javascript c# asp.net ajax excel


    【解决方案1】:

    出于安全原因,您不能从客户端下载文件。我建议使用隐藏按钮服务器端并模拟来自另一个按钮客户端的点击。

    问候。

    【讨论】:

      猜你喜欢
      • 2013-05-16
      • 2011-10-03
      • 1970-01-01
      • 1970-01-01
      • 2016-07-08
      • 2011-10-16
      相关资源
      最近更新 更多