通过ajax下载创建的文件

时间:2014-10-12 22:33:51

标签: javascript php jquery ajax wordpress

我在字体结尾有这个表格:

<form id="some_info" method="post" action="#">
    <input type="text" name="info" id="info" value="">
    <input type="hidden" name="action" id="action" value="my_down_action">
    <input type="file" name="test">
    <input id="submit-ajax" name="submit-ajax" type="submit" value="dwonload">
<form>

当点击提交时,ajax通过以下代码将表单发送到php函数my_down_action

var options = { 
    success: showResponse,   
    url:  ajaxurl           
}; 

// bind form using 'ajaxForm' 
$('#some_info').ajaxForm(options); 

function showResponse(responseText, statusText, xhr, $form)  {
//download the text
}

这是php函数:

//hook the Ajax call
//for logged-in users
add_action('wp_ajax_my_down_action', 'my_down_action');
//for none logged-in users
add_action('wp_ajax_nopriv_my_down_action', 'my_down_action');

function my_down_action(){
    $ttext = $_POST['info'];
    header('Content-Type: application/txt');
    header('Content-Disposition: attachment; filename=info.txt');
    header('Pragma: no-cache');
    echo $ttext;
    die();
}

如何在ajax中下载响应?

0 个答案:

没有答案