小编Mok*_*bli的帖子

通过ajax调用ASP MVC下载PDF文件

我尝试下载一个pdf文件按钮点击通过ajax调用使用ASP MVC模型当我点击我的按钮,什么都没发生,但当我在网址上添加控制器方法我的文件下载.我只想在按钮点击下载它

JS:

$('#PrintTimeSheet').click(function () {
            $.ajax({
                type: 'POST',
                url: "/Home/DownloadFile",
                success: function (response) {
                }
            });
});
Run Code Online (Sandbox Code Playgroud)

控制器:

public FileResult DownloadFile()
{
    Document PDF = new Document();
    MemoryStream memoryStream = new MemoryStream();
    PdfWriter writer = PdfWriter.GetInstance(PDF, memoryStream);
    PDF.Open();
    PDF.Add(new Paragraph("Something"));
    PDF.Close();
    byte[] bytes = memoryStream.ToArray();
    Response.ContentType = "application/pdf";
    Response.AddHeader("Content-Disposition", "attachment;filename=Receipt-test.pdf");
    Response.BinaryWrite(memoryStream.ToArray());
    return File(bytes, "application/pdf");
}
Run Code Online (Sandbox Code Playgroud)

ajax asp.net-mvc

1
推荐指数
1
解决办法
8735
查看次数

标签 统计

ajax ×1

asp.net-mvc ×1