Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.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# 从jquery下载具有现有路径的文件的最简单方法是什么?_C#_Javascript_Jquery_Asp.net Mvc - Fatal编程技术网

C# 从jquery下载具有现有路径的文件的最简单方法是什么?

C# 从jquery下载具有现有路径的文件的最简单方法是什么?,c#,javascript,jquery,asp.net-mvc,C#,Javascript,Jquery,Asp.net Mvc,我浏览了十几页关于“如何从jQuery下载文件”的问题,但仍然没有找到简单的解决方案 我的jQuery内置了ajax: $.ajax({ url: "/Home/SaveQBMatter", type: "POST", data: JSON.stringify({ 'Matters': result, 'originalRecords': originalRecords, 'originalHours': originalHours, 'excludedHours'

我浏览了十几页关于“如何从jQuery下载文件”的问题,但仍然没有找到简单的解决方案

我的jQuery内置了ajax:

$.ajax({
     url: "/Home/SaveQBMatter",
     type: "POST",
     data: JSON.stringify({ 'Matters': result, 'originalRecords': originalRecords, 'originalHours': originalHours, 'excludedHours': excludedHours, 'totalHours': totalHours }),
     dataType: "json",
     traditional: true,
     contentType: "application/json; charset=utf-8",
     success: function (data) {
           if (data.status == "Success") {
                var DownloadableFile = data.message;

                //HERE I NEED TO DOWNLOAD FILE

                alert("Success! You will be redirect to the Home Page.");
                var url = '@Url.Action("Index", "Home")';
                window.location.href = url;
           } else {
                alert("Error occurs on the Database level!");
           }
     },
     error: function () {
           alert("An error has occured!!!");
     }
});
这里是
data.message
我正在从操作
SaveQBMatter
完整文件路径返回

我需要的只是让我的用户在重定向之前下载这个文件。需要帮忙吗


注意:我正在使用ASP.NET MVC,如果需要此信息,请尝试使用此插件下载文件:

您可以这样做,在ajax调用成功后,执行location.href=data.message;正如您所说,data.message是文件的完整路径。这样,它就可以在不重定向浏览器的情况下下载文件。此外,在下载文件时,请确保具有强制下载的头文件

您可以在此处查看有关强制下载的更多信息:

然后,设置超时时间,比如说1到2秒,你可以根据自己的喜好调整时间,让下载初始化并重定向。因此,您的代码如下所示:

$.ajax({
     url: "/Home/SaveQBMatter",
     type: "POST",
     data: JSON.stringify({ 'Matters': result, 'originalRecords': originalRecords, 'originalHours': originalHours, 'excludedHours': excludedHours, 'totalHours': totalHours }),
     dataType: "json",
     traditional: true,
     contentType: "application/json; charset=utf-8",
     success: function (data) {
           if (data.status == "Success") {
                var DownloadableFile = data.message;

                location.href = DownloadableFile;

                setTimeout(function() {    
                    alert("Success! You will be redirect to the Home Page.");
                    var url = '@Url.Action("Index", "Home")';
                    window.location.href = url;
                }, 1000);
           } else {
                alert("Error occurs on the Database level!");
           }
     },
     error: function () {
           alert("An error has occured!!!");
     }
});

如果未被用户浏览器阻止,请尝试在新窗口中打开URL:

if (data.status == "Success") {
    window.open(data.message, "_blank")
如果打开文件而不是下载文件时遇到问题,可以尝试从ASP发送内容处置标头,如:

Content-Disposition: attachment; filename="filename.extension"

使用文件的路径打开一个新标签,这将是非常棒的!如果你能帮我处理iframe和cookies之类的东西…data.message有什么功能?URL或文件的实际内容?基本上,让为客户端提供文件的脚本设置cookie,然后递归地检查所述cookie的存在。当它存在时,下载就完成了,你可以重定向。@KevinB,文件下载不会停止,即使你离开了一个页面。这段代码打开了一个窗口,其中url是我的文件的路径,但下载没有启动。地址不清楚。Firefox不知道如何打开这个地址,因为协议(c)与任何程序都没有关联。如果您可以调整data.message的响应以返回整个url路径,它应该可以正常工作。就像我应该在ASP.NET中把{Content-Disposition:attachment;filename=“filename.extension”}放在哪里一样,您应该调用
Response
对象的
(HttpContext.)Response.Headers.add(“Content-Disposition”,“attachment;filename=\”filename.extension\”)方法