首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AJAX-发布文件后,文件没有下载

AJAX-发布文件后,文件没有下载
EN

Stack Overflow用户
提问于 2017-05-30 07:21:15
回答 1查看 782关注 0票数 1

如果我直接进入我的程序的URL,它生成一个PDF,我得到一个直接下载。

代码语言:javascript
复制
 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-网站,加载所有数据和功能与页面_ load事件.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-30 07:30:43

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

代码语言:javascript
复制
            $.ajax({
                url: "THE_POST_URL",
                method: 'POST',
                success: function (response) { //return the download link
                     window.location.href = response;
                },
                error: function (e) {
                },
            });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44255958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档