C# 如何调用MVC操作来下载PDF文件?

C# 如何调用MVC操作来下载PDF文件?,c#,ajax,file,model-view-controller,download,C#,Ajax,File,Model View Controller,Download,我调用一个MVC操作,它创建一个内存PDF文件。我想在完成操作后立即返回文件并下载 调用MVC操作的Ajax代码 function convertToPDF() { $.ajax({ url: "/Tracker/ConvertPathInfoToPDF", type: "GET", data: JSON.stringify({ 'pInfo': null }), dataType:

我调用一个MVC操作,它创建一个内存PDF文件。我想在完成操作后立即返回文件并下载

调用MVC操作的Ajax代码

function convertToPDF() {
        $.ajax({
            url: "/Tracker/ConvertPathInfoToPDF",
            type: "GET",
            data: JSON.stringify({ 'pInfo': null }),
            dataType: "json",
            traditional: true,
            contentType: "application/json; charset=utf-8",
            success: function (data) {

            },
            error: function () {
                alert("Unable to call /Tracker/ConvertPathInfoToPDF");
            }
        });
    }
public FileResult ConvertPathInfoToPDF(PositionInfo[] pInfo)
    {
        MemoryStream fs = new MemoryStream();
        //FileStream fs = new FileStream(@"C:\Test.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
        Rectangle rec = new Rectangle(PageSize.A4);
        Document doc = new Document(rec);
        PdfWriter writer = PdfWriter.GetInstance(doc, fs);
        doc.Open();
        doc.Add(new Paragraph("Hamed!"));
        doc.Close();

        return File(fs, System.Net.Mime.MediaTypeNames.Application.Octet, "Test.pdf");
    }
MVC行动

function convertToPDF() {
        $.ajax({
            url: "/Tracker/ConvertPathInfoToPDF",
            type: "GET",
            data: JSON.stringify({ 'pInfo': null }),
            dataType: "json",
            traditional: true,
            contentType: "application/json; charset=utf-8",
            success: function (data) {

            },
            error: function () {
                alert("Unable to call /Tracker/ConvertPathInfoToPDF");
            }
        });
    }
public FileResult ConvertPathInfoToPDF(PositionInfo[] pInfo)
    {
        MemoryStream fs = new MemoryStream();
        //FileStream fs = new FileStream(@"C:\Test.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
        Rectangle rec = new Rectangle(PageSize.A4);
        Document doc = new Document(rec);
        PdfWriter writer = PdfWriter.GetInstance(doc, fs);
        doc.Open();
        doc.Add(new Paragraph("Hamed!"));
        doc.Close();

        return File(fs, System.Net.Mime.MediaTypeNames.Application.Octet, "Test.pdf");
    }
MVC操作完全运行,但我在浏览器中收到以下错误:

加载资源失败:服务器响应状态为500(内部服务器错误)

MVC行动:

public FileResult ConvertPathInfoToPDF(PositionInfo[] pInfo)
    {
        MemoryStream fs = new MemoryStream();
        Rectangle rec = new Rectangle(PageSize.A4);
        Document doc = new Document(rec);
        PdfWriter writer = PdfWriter.GetInstance(doc, fs);
        doc.Open();
        doc.Add(new Paragraph("Hamed!"));
        doc.Close();

        byte[] content = fs.ToArray(); // Convert to byte[]

        return File(content, "application/pdf", "Test.pdf");
    }
调用MVC操作的Ajax代码:

function convertToPDF() {
        window.location = '/Tracker/ConvertPathInfoToPDF?pInfo=null';
    }
可能重复的