下载文件虽然AJAX POST文件、AJAX、POST

2023-09-10 14:35:57 作者:BLUE 16°深蓝梦境

我试图提供使用AJAX POST请求一个简单的下载。用户点击<跨度> 并开始下载(或下载对话框出现时,根据不同的浏览器)。我实现了这一切,但Ajax响应不会触发任何浏览器的下载行为,我不知道为什么。

I'm trying to provide a simple download using AJAX POST request. A user clicks a <span> and the download begins (or a download dialog shows up, depending on the browser). I've implemented all this, but the AJAX response doesn't trigger any browser download behavior and I don't know why.

下面是我的code:

HTML /枝:

<span id="export_csv">csv - click me to download the file</span>

JavaScript的:

javascript:

<script type="text/javascript">
$(document).ready(function() {
  $('#export_csv').click(function(){
    var params = "some params";
    $.ajax({
      type: 'POST',
      url:  "{{path('ExportBundle_export', {'format': 'csv','report' : 'basic'})}}",
      data: params
    });
  });
});
</script>

控制器code(Symfony2的,但它并不重要位置):

Controller code (symfony2, but it doesn't matter here):

$response = new Response(file_get_contents($document->getPath()));
$response->setStatusCode(200);
$response->headers->set('Content-Type', $document->getMime());
$response->headers->set('Content-Description', 'Submissions Export');
$response->headers->set('Content-Disposition', 'attachment; filename='
    . $filename . '.' . $format);
$response->headers->set('Content-Transfer-Encoding', 'binary');
$response->headers->set('Pragma', 'no-cache');
$response->headers->set('Expires', '0');
return $response;

这是例子请求:

Request URL:http://myapp.local/app_dev.php/export/basic/csv
Request Method:POST
Status Code:200 OK

和响应报头:

cache-control:private, must-revalidate
Connection:close
content-description:Submissions Export
content-disposition:attachment; filename=report_1363013244.csv
Content-Length:790
content-transfer-encoding:binary
Content-Type:text/csv; charset=UTF-8
Date:Mon, 11 Mar 2013 14:47:24 GMT
expires:0
pragma:no-cache
Server:Apache/2.2.22 (Ubuntu)
x-debug-token:513dee7c99fdb
X-Powered-By:PHP/5.3.10-1ubuntu3.5

在谷歌浏览器萤火酷似工具栏和响应

我检查网​​络设置页长相酷似文件。也许有什么不对我的头,但我检查了两次了......

I've checked Network tab in Google Chrome firebug-alike toolbar and the response looks exactly like the file. Maybe there is something wrong with my headers, but I checked them twice already...

PS 1

我想使用AJAX它来作(I preFER它比创建一个HTML表单)。

I'd like to make it using AJAX (I prefer it more than creating a HTML form).

PS 2

我有很多的客户端层(JavaScript的)的参数。我需要所有这些是在控制器(服务器端脚本)可用。使用AJAX,我只是把它们在数据键。

I've got lots of parameters in the client side layer (javascript). I need all of them to be available in the controller (server-side script). When using AJAX, I just pass them in the data key.

推荐答案

我不明白你为什么要触发一个Ajax调用下载? 你为什么不只是做一个普通的&LT; A HREF =下载/我&GT;下载&LT; / A&GT; 并设置页面的MIME类型。这样你可以运行任何PHP code,然后呼应MIME类型,这不会导致页面重新加载,你会留在页面从那里你做了点击。

I do not get why you want to trigger an ajax call for download? Why don't you just do a regular <a href="download/me">Download</a> and set the mime type of that page. This way you can run any php code and then echo a mime type, this will not cause the page to reload and you will stay in the page from where you did the click.

http://davidwalsh.name/php-header-mime