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:

   $http({
      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 $window.open('./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?

Edit:

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

JavaScript:

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

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

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

    form.appendChild(input);
    form.appendChild(submit);
    document.body.appendChild(form);

    $('#submitProject').click();

    document.body.removeChild(form);

      

PHP:

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

        // ...

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

        echo $json;

?>

      

+3


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

.

+2


source







All Articles