Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法在客户端(MVC)下载文件_C#_.net_File_Model View Controller_Download - Fatal编程技术网

C# 无法在客户端(MVC)下载文件

C# 无法在客户端(MVC)下载文件,c#,.net,file,model-view-controller,download,C#,.net,File,Model View Controller,Download,当我试图将excel文件从服务器下载到客户端时,没有收到任何响应。我有下面的代码来做同样的事情 public ActionResult Download() { string file = "testfile.xlsx"; string fullPath = Path.Combine(Server.MapPath("~/temp"), file); return File(fullPath, "application/vnd.openxmlformats-office

当我试图将excel文件从服务器下载到客户端时,没有收到任何响应。我有下面的代码来做同样的事情

public ActionResult Download()
{
    string file = "testfile.xlsx";

    string fullPath = Path.Combine(Server.MapPath("~/temp"), file);

    return File(fullPath, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", file);
}
我尝试将contenttype更改为“application/vnd.msexcel”,但仍然不起作用

代码执行此操作,但解析返回文件后不会发生任何事情。

我还将为此操作添加ajax调用

$('#Submit_Report').click(function (event) {
if (($('#txtReportMarket').val() == '') && ($('#txtReportPractice').val() == '')) {
            alert("Please enter a value to Generate Report based on!");
            event.preventDefault();
        }

        else {
            $.ajax({
                url: '/ShareTemplate/GenerateReport/',
                data: "{ 'ReportPractice': '" + $('#txtReportPractice').val() + "','ReportMarket':'" + $('#txtReportMarket').val() + "'}",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    alert(data["FileName"]);
                    $.ajax({
                        type: 'GET',
                        url: '/ShareTemplate/Download/',
                        success: function (data) {
                            alert("download Successful!");
                            @*window.open('@Url.Content("~/ShareTemplate/Download")')*@
                        },
                        error: function () {
                            DisplayError('Failed to load the data.');
                        }
                    });
                }
            });
        }
    });

第一个ajax调用工作正常,文件已经生成并存储。但是第二个ajax调用就是问题所在,有时我也会从第二个ajax调用中获得成功消息,但fle仍然没有下载。

在第一个
ajax
调用中
success
。放置下面的代码并删除剩余的代码

window.location = '@Url.Action("Download", "ShareTemplate")';