最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular $http POST to target="_blank" - Stack Overflow

programmeradmin1浏览0评论

I need to make a POST from Angular to a URL ./makeFile.php which will create a file with contents from a database query based on the information provided in that POST data.

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

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

But making a POST from AngularJS using $http forces the browser to not open this save dialog:

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

I can't make a GET because the data is too long for a URL, otherwise I would replace it with a simple $window.open('./makeFile.php', '_blank') which works in (the rare) case of small data.

How do I make Angular make this POST to another window or let the browser open the save dialog?

Edit:

As Ivan suggested, I had to programatically create a form to achieve what I want. Here's 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;

?>

I need to make a POST from Angular to a URL ./makeFile.php which will create a file with contents from a database query based on the information provided in that POST data.

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

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

But making a POST from AngularJS using $http forces the browser to not open this save dialog:

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

I can't make a GET because the data is too long for a URL, otherwise I would replace it with a simple $window.open('./makeFile.php', '_blank') which works in (the rare) case of small data.

How do I make Angular make this POST to another window or let the browser open the save dialog?

Edit:

As Ivan suggested, I had to programatically create a form to achieve what I want. Here's 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;

?>
Share Improve this question edited Apr 23, 2015 at 13:27 Bruno Finger asked Apr 23, 2015 at 12:34 Bruno FingerBruno Finger 2,6033 gold badges29 silver badges50 bronze badges 2
  • How do you call your $http in your app ? Using a model, controller and so on ? – Guilherme Ferreira Commented Apr 23, 2015 at 12:36
  • Display "download" link on success with target="_blank" or window.open. – Ruslanas Balčiūnas Commented Apr 23, 2015 at 12:40
Add a ment  | 

1 Answer 1

Reset to default 6

I had similar issue, and I couldn't do with with $http. So I programatically created html form with method="post" and target="_blank". And I placed hidden elements with data I needed to send. After I submitted that form I removed it from dom.

Let me know if you find another solution, using $http.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>