2012-09-22 64 views
0

我在ajax事件服务器端创建一个word文档,并作为响应如果没有错误我想推它下载。是否有可能与jQuery的Ajax?我的代码看起来在这一刻,因为它遵循jquery ajax响应下载文件

   $.ajax({ 
         type: "POST", 
         url: 'docsx.php', 
         data: $("#edit_form").serialize(), // serializes the form's elements. 
         success: function(data) 
         { 
         **here should be fired the download** // show response from the php script. 
         }, 
         error:function(){ 

          alert('Es gibt ein Fehler bei Daten übetragung!'); 

         } 
        }); 
+2

你可以只重定向到一个网页,会强制下载(通过设置适当的头)。另一种选择是使用[this](http://filamentgroup.com/lab/jquery_plugin_for_requesting_ajax_like_file_downloads/),但不幸的是它还没准备好。看起来他们还在试验它。 –

+0

感谢您的反馈,我以前一直在看你的建议链接,但没有在我的案件工作,但是你的第一个建议是做我想做的。非常感谢 – kakuki

回答

0

我曾经做过自己:您可以生成PHP文件,把它放在你的服务器上的某个地方,然后路径返回到您的脚本。接下来,创建一个隐藏的iframe,并调用一个简单的脚本,该脚本只是读取文件并将其返回并设置相应的标题以强制下载。结果将是不刷新页面的强制下载。

+1

在我的debian环境中,这段代码一直在做相应的工作window.location.href ='to_download.docx'; – kakuki

0

您无法使用JavaScript来触发下载。只是不要为此使用AJAX。使用标准的形式提交给docsx.php脚本:

<form action="docsx.php" method="post"> 
    ... some input fields 

    <input type="submit" value="Download" /> 
</form> 
+0

这个工作有点复杂我有一个jQuery UI对话框,并且在按钮事件中,我激发了很多任务,并且我不想让用户跳出对话框。 – kakuki

+1

如果您的服务器端脚本直接使用Content-Disposition:attachment将文件传输到响应,则不会跳出对话框; filename = foobar.txt'头。这将直接提示用户选择一个位置来将文件保存到他的计算机上,而无需离开当前页面。 –

+0

好的,谢谢我要尝试一下,但是这个window.location.href ='to_download.docx'怎么样。文档正在创建,并且我正在使用此片段作为ajax响应下载。在这种情况下会有怀疑吗? – kakuki

0
<div id="Status_Update" title="Update your Status Here"> 
    <form method="post" action="update_status.php?<?php echo(SID); ?>" name="Status_form" id="Status_form"> 
     <span id="sprytextfield1"> 
     <label for="text1"></label> 
     <label for="textfield6"></label> 
     <textarea class="ns_tb" name="status_update_box" cols="60" placeholder="What is on your mind" style="font-family:'Lucida Sans Unicode', 'Lucida Grande', sans-serif; border: thin solid #0C0; border-radius:9px;"></textarea> 
<span class="textfieldRequiredMsg">A value is required.</span></span> 
     <input type="submit" name="status_update_button" id="button" value="Post" style=" background-color:#0C0; border-radius:9px;)"/> 
     <a href="#" id="hide_status_box" tittle="Hide the Status Comment Box">hide</a> 
    </form> 
    </div>