无法使用Web Service C#从服务器下载Excel文件

问题描述:

我想在客户端计算机上下载Excel文件.我正在使用Web Service C#.使用JQuery AJAX调用,我先调用Web服务,然后再下载excel文件.

I want to download Excel file on Client's machine. I'm using Web Service C#. Using JQuery AJAX call I'm calling Web Service and then I want to download excel file.

但是问题是,当我直接在浏览器中运行Web Service时,它正在下载,但是使用AJAX调用时却未下载.

But the issue is when I'm running Web Service directly in the browser it's downloading but with AJAX call it's not downloading.

这是我的代码:

C#:

    [WebMethod]
    public void DownloadExcel(string fileName)
    {
        string filePath = "~/Report/" + fileName;

        Context.Response.Clear();
        Context.Response.Buffer = true;
        Context.Response.Charset = "";
        Context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Context.Response.AddHeader("content-disposition", "attachment;filename=" + fileName + ";");
        Context.Response.TransmitFile(Server.MapPath(filePath));
        Context.Response.Flush();
        Context.Response.End();
    }

JS:

    var FileName = "Test.xlsx"

    $.ajax({
        type: "POST",
        data: { "fileName": FileName },
        url: "API/MyService.asmx/DownloadExcel",
        dataType: 'json',
        success: function (data) {
              $('#MessageModal').modal('show');
        },
        error: function (request, error) {

        }
    });

Ajax请求可能正在下载文件,但是浏览器并未将其解释为文件下载(因为使用ajax调用,您自己来处理响应)

The Ajax request is probably downloading the file, but the browser is not interpreting it as a file download (because with ajax calls, you are handling the response yourself)

这是一个示例页面,可以解决您的老式问题.

Here is a sample page, solving your issue old-school.

请注意在页面上将< Form> 元素与 id ="excelDownloadForm" 一起使用.它没有 runat ="Server" 属性.

Please note the use of a <Form> element with id="excelDownloadForm" on the page. It does not have the runat="Server" attribute.

在Asp.Net之后,我们习惯于每页仅使用一种形式(runat ="Server").但是实际上,您的HTML页面中可以有多种形式,并且可以将不同的数据束发布到不同的端点.

After Asp.Net, we are accustomed to having only one form (runat="Server") per page. But actually, you can have multiple forms in your HTML page and can post different bunches of data to different endpoints.

这里唯一的缺点是您无法处理成功的响应,并且无法在传输完成时显示模式.

The only drawback here is that you cannot handle the successful response and show a modal when the transfer completes.

请让我知道您对这种方法的看法以及是否有帮助.

Please let me know what you think of this approach and if it helps.

(注意:经过全面测试,可以正常工作)

(Note: Fully tested and working)

<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery.js"></script>
</head>
<body>
    <form id="excelDownloadForm" action="API/MyService.asmx/DownloadExcel" method="post" target="_self" >
        <input type="hidden" name="fileName" id="fileName" />
    </form>
    <form id="form1" runat="server">
    <div>
        <script>
            var FileName = "test.xlsx"
            $("#fileName").val(FileName);
            $("#excelDownloadForm").submit();
        </script>
    </div>
    </form>
</body>
</html>