如果我直接进入我的程序的URL,它生成一个PDF,我得到一个直接下载。
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事件.
发布于 2017-05-30 07:30:43
这样做的一种方法是点击ajax帖子,在调用成功后返回文件下载链接,然后将浏览器重定向到该链接,下载文件如下:
$.ajax({
url: "THE_POST_URL",
method: 'POST',
success: function (response) { //return the download link
window.location.href = response;
},
error: function (e) {
},
});https://stackoverflow.com/questions/44255958
复制相似问题