1

I am returning a file from an ASP.NET MVC controller and I want to download it as an attachment using AngularJS.

MVC Controller:

return File(renderedBytes, mimeType);

AngularJS:

function generatePDF(reportRequest) {

            var reportController = "Report";
            var controllerUrl = ngAppSettings.homeBaseUri + reportController + "/GeneratePDF";
            var deferred = $q.defer();
            $http({
                method: "POST",
                url: controllerUrl,
                data: reportRequest
            }).success(function (data, status, headers, cfg) {
                //window.open(controllerUrl, "_self", "");
                deferred.resolve(data);
            }).error(function (err, status) {
                deferred.reject(err);
            });

            return deferred.promise;

        };

I've tried various ways as suggested on the similar questions about this but I'm not getting it.

I've tried this too.

My breakpoints hit without any problems inside the GeneratePDF controller but nothing happens after that. How would I go about this?

EDIT:

I also tried angular file saver but I'm getting an error that the format should be in BLOB. If I can only convert the response to a blob then I think this might be it.

TIA

1 Answer 1

1

If your C# Controller endpoint returns a FileContentResult (which it appears that it does), then you shouldn't need to worry about promises. You can simply do the following...

function generatePDF(reportRequest) {
            var reportController = "Report";
            var controllerUrl = ngAppSettings.homeBaseUri + reportController + "/GeneratePDF";
            window.open(controllerUrl, "_blank");
        };

I am doing this in one of my apps and it is working as expected. Is there a particular reason you were using the $q service to return a promise?

Sign up to request clarification or add additional context in comments.

4 Comments

It's just the design of all my angular services uses $q service which I can ignore if this is an issue. Using window.open, how would I pass my reportRequest object as parameter for the GeneratePDF controller? I need it for data processing.
Ah sorry, overlooked that bit. Is there a lot of data in the reportRequest or is it something that could be represented in a "GET" query string (aka just basic primitives that you can make a query string out of)? Unfortunately I'm not aware of a way to do a "POST" to download the doc. If you have to send a ton of data back to process in order to generate the pdf, you'll either need to do that and have the server pass back a token that you can then use to do a GET (using window.open) that will then return the FileResult that is cached on the server in some way.
I can't pass all the data as a query string as the request contains a list of objects. I was planning to do that too at first but didn't proceed.
Just to close this thread. Your comment pointed me to the right path and ended up making a POST to generate the document and a GET to download it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.