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

时间:2016-07-02 07:08:32

标签: c# ajax file model-view-controller download

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

调用MVC操作的Ajax代码

<!DOCTYPE html>
<html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.js"></script>
<script>


      angular.module('mainModule', [])
         .controller('MainCtrl', ['$scope', '$rootScope', '$window', function($scope, $rootScope, $window) {
        $scope.visit = {hours: 1200};

         }])

.component('inputNumber', {
    template: '<input ng-model="$ctrl.outerModel" ng-required="$ctrl.required" ng-change="$ctrl.test();" name="$ctrl.name">',
    bindings: {
        'step': '<',
        'min': '<',
        'max': '<',
        'required': '<',
        'disableText': '<',
        'resetOnOverflow': '<',
        'outerModel': '=',
        'changeCallback': '&',
        'doubledZero': '<',
    },
    controller: function($rootScope, $scope, $element) {
        let ctrl = this;
        let input = $element.find('input');
        ctrl.changeCallback = ctrl.changeCallback();

        ctrl.descritizeUnceratainVisitMinutes = function (value) {
            if (+value > 0 && +value <= 15) {
                ctrl.outerModel = 15;
            } else if (+value > 15 && +value <= 30) {
                ctrl.outerModel = 30;
            } else if (+value > 30 && +value <= 45) {
                ctrl.outerModel = 45;
            } else {
                ctrl.outerModel = 0;
            }

            return ctrl.outerModel;
        };

        ctrl.test = function () {
            alert(123);
            console.log(123);
        }
    }
});
    </script>
<body ng-app="mainModule">
<div ng-controller="MainCtrl">
<input-number
                    class="e-time-part__uncertain-visit"
                    min="0"
                    required="true"
                    disable-text="true"
                    reset-on-overflow="true"
                    outer-model="visit.hours"
                    change-callback="changeVisitCallback"
                    name="hours"
                >
</div>

</body>
</html>

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");
            }
        });
    }

MVC操作是完全运行的,但我在浏览器中收到以下错误:

  

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

1 个答案:

答案 0 :(得分:4)

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';
    }