Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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
Javascript 在AJAX Post之后,文件不会下载_Javascript_C#_Asp.net_Json_Ajax - Fatal编程技术网

Javascript 在AJAX Post之后,文件不会下载

Javascript 在AJAX Post之后,文件不会下载,javascript,c#,asp.net,json,ajax,Javascript,C#,Asp.net,Json,Ajax,如果我从生成PDF的程序直接转到URL,我会直接下载 public void Download(byte[] file) { try { MemoryStream mstream = new MemoryStream(file); long dataLengthToRead = mstream.Length; Response.Clear(); Response.ClearContent(); R

如果我从生成PDF的程序直接转到URL,我会直接下载

 public void Download(byte[] file)
{

    try
    {
        MemoryStream mstream = new MemoryStream(file);
        long dataLengthToRead = mstream.Length;
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.BufferOutput = true;
        Response.ContentType = "Application/pdf"; /// if it is text or xml
        Response.AddHeader("Content-Disposition", "attachment; filename=" + "Test.pdf");
        Response.AddHeader("Content-Length", dataLengthToRead.ToString());
        mstream.WriteTo(Response.OutputStream);
        Response.Flush();
        Response.Close();
        HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
        HttpContext.Current.Response.SuppressContent = true;  // Gets or sets a value indicating whether to send HTTP content to the client.
        HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.
    }
    catch (Exception e)
    {

    }
}
但是如果我用我的JSON发布到URL,这个方法不会给我直接下载


带有JSON字符串的帖子已成功。但是下载不会在Ajax调用之后开始。我的程序是一个简单的ASPX网站,通过Page_load-Event加载所有数据和函数。

一种方法是点击ajax帖子,在调用成功后返回文件下载链接,然后将浏览器重定向到该链接以下载文件,如下所示:

            $.ajax({
                url: "THE_POST_URL",
                method: 'POST',
                success: function (response) { //return the download link
                     window.location.href = response;
                },
                error: function (e) {
                },
            });

非常有用的答案!但我能把它改为直接下载,而不是在浏览器中打开PDF吗?@PeterH。您不能使用AJAX直接下载文件。@PeterH您可以查看@ShaktiPhartiyal返回的响应是什么@MahmodAbuJehad响应将是来自api/url的响应,在这种特定情况下,它应该是下载链接