提示用户通过AJAX调用保存的文件 [英] Prompt user to save file through AJAX call
问题描述
我出口我DHTMLX电网CSV和成功能够创建.csv文件。时遇到的问题是,它没有提示用户保存/打开该文件。我使用的是从JavaScript一个$。员额调用CSV字符串发送到PHP,然后写该字符串为CSV。出于某种原因,它不产生对用户的提示,但它被成功写入文件并保存在服务器上。下面是相关的code:
I'm exporting my DHTMLX grid to csv and have successfully been able to create the .CSV file. The problem I'm having is that it isn't prompting the user to save/open the file. I'm using a $.post call from javascript to send the CSV string to PHP, then writing that string to csv. For some reason it isn't creating a prompt for the user, but it is successfully writing the file and saving on the server. Below is the relevant code:
记者:
myGrid.csvParser = myGrid.csvExtParser;
myGrid.setCSVDelimiter('|');
myGrid.csv.row = "endOfRow";
var gridCsvData = myGrid.serializeToCSV();
$.post(
"data/export.php",
{
csvdata: gridCsvData
}
);
PHP(export.php):
PHP (export.php):
$csvData = $_REQUEST['csvdata'];
$csv = explode('endOfRow',$csvData);
$myfile = "grid.csv";
$fh = fopen($myfile, 'w') or die("can't open file");
foreach($csv as $line) {
fputcsv($fh, explode('|',$line),',','"');
}
fclose($fh);
//Redirect output to a client's web browser (csv)
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=grid.csv");
header("Pragma: no-cache");
header("Expires: 0");
这code完美的作品,因为它出口到底我想要的网格,并将其保存为grid.csv的意义。的问题是,它没有提示用户保存文件。这是一个问题,我的PHP头或者我需要把一些在$。员额提示成功?感谢您的帮助!
This code works perfectly in the sense that it exports the Grid exactly how I want it and saves it to 'grid.csv'. The problem is that it isn't prompting the user to save the file. Is this a problem with my PHP headers or do I need to put something in the $.post to prompt on success? Thanks for any help!
推荐答案
您不能及时从AJAX调用下载文件的用户。有一件事你可以做的是,让一个IFRAME,把一个形式,然后张贴。这样的话,它会看起来像一个AJAX调用,但用户将被提示下载文件。
You can't prompt the user to download a file from an AJAX call. One thing you can do is, make an iFrame, put a form in it, then POST it. That way, it'll look like an AJAX call, but the user will be prompted to download the file.
// Create iFrame
var iframe = document.createElement('iframe');
iframe.style.display = "none";
document.body.appendChild(iframe);
// Get the iframe's document
var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
// Make a form
var form = document.createElement('form');
form.action = 'data/export.php'; // Your URL
form.method = 'POST';
// Add form element, to post your value
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'csvdata';
input.value = gridCsvData; // Your POST data
// Add input to form
form.appendChild(input);
// Add form to iFrame
// IE doesn't have the "body" property
(iframeDoc.body || iframeDoc).appendChild(form);
// Post the form :-)
form.submit();
P.S。你的PHP code实际上并没有呼应CSV屏幕,它只是将其保存到一个文件中。
P.S. Your PHP code doesn't actually echo the CSV to the screen, it just saves it to a file.
在头电话,请确保您有:
After the header calls, make sure you have:
readfile($myfile);
这篇关于提示用户通过AJAX调用保存的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!