Angular $ http POST for target = "_blank"

I need to do POST

from Angular to a URL ./makeFile.php

that will create a file with content from a database query based on the information provided in the data POST


PHP forces the browser to open a save dialog rather than just displaying a response with these two lines:

header('Content-Disposition: attachment; filename="' . $file_name . '.prj"');
echo $json;


But building POST

from AngularJS using $http

makes the browser not open this save dialog:

      method: 'POST',
      url: './makeFile.php',
      data: {
        project: ProjectService.getProject()


I can't do GET

it because the data is too long for the url, otherwise I would replace it with a simple $'./makeFile.php', '_blank')

one that works in the (rare) case of small data.

How do I make Angular do it POST

in another window, or open the browser in a save dialog?


As Ivan said, I had to programmatically create a form to achieve what I want. This is how I did it:


    var form = document.createElement('form');
    form.action = './php/saveProject.php';
    form.method = 'POST'; = '_blank'; = 'none';

    var input = document.createElement('input');
    input.type = 'text'; = 'project';
    input.value = angular.toJson(ProjectService.getProject(), false);

    var submit = document.createElement('input');
    submit.type = 'submit'; = 'submitProject';






        $project = get_magic_quotes_gpc() ? stripslashes($_POST['project']) : $_POST['project'];

        // ...

        header('Content-Disposition: attachment; filename="' . $file_name . '.prj"');

        echo $json;




source to share

1 answer

I had a similar problem and couldn't work with it $http

. So I programmatically created an html form with method="post"

and target="_blank"

. And I put hidden items with the data I needed to send. After I submitted this form, I removed it from the dom.

Let me know if you find another solution using $http




All Articles