How to capture file save as cancel respond
I followed this post http://www.the-art-of-web.com/php/dataexport/ and successfully create an export to csv file from DB, base on user current search view. But to prevent hit to db while export is happening so I disable the export unless there is change within the view or user query. The issue I face with is when user hit on export a file save as confirm dialog pop up and if user changed her mind click on cancel. The export button remain disable. The only way for user to get back is to change query and come back again.
My question is there anyway I could capture the cancel click respond on file save as confirm dialog.
Thanks
The code is very similar to http://www.the-art-of-web.com/php/dataexport/ Where as the view would be a grid with bunch of customer information from given date range. If user like the view they choose. They would click on export button.
sample code export.php
function exportCSV(){
document.getElementById("exportCSV").src = "test1.php";
document.getElementById("exportBtn").disabled = true;
}
function performSearch(){
//perform search get result and display
//if resultset length > 0
document.getElementById("exportBtn").disabled = false;
}
Grid display right here <button id="search" onclick="performSearch()">Search
<button id="exportBtn" onclick="exportCSV()"> Export <iframe id="exportCSV" style="display:none"/>test1.php
$data = array(
array("firstname" => "Mary", "lastname" => "Johnson", "age" => 25), array("firstname" => "Amanda", "lastname" => "Miller", "age" => 18), array("firstname" => "James", "lastname" => "Brown", "age" => 31), array("firstname" => "Patricia", "lastname" => "Williams", "age" => 7), array开发者_C百科("firstname" => "Michael", "lastname" => "Davis", "age" => 43), array("firstname" => "Sarah", "lastname" => "Miller", "age" => 24), array("firstname" => "Patrick", "lastname" => "Miller", "age" => 27) );# filename for download $filename = "website_data.xls";
header("Content-Disposition: application/octet-stream; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");$flag = false;
foreach($data as $row) { if(!$flag) { # display field/column names as first row echo implode("\t", array_keys($row)) . "\n"; $flag = true; } array_walk($row, 'cleanData'); echo implode("\t", array_values($row)) . "\n"; } exit; function cleanData(&$str)
{
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}
There's no way to capture the browser event for canceling a save file. Using a confirm in an if statement(or something similar) is probably the best ux for that situation:
if(confirm('are you sure you want to export?'))
{
//export code
}
else
{
//cancel code
}
If you want your export button to become re-enabled I would call a re-enable function whenever your user's search(or whatever action they make to change data) is called. Or you can also use setTimeout() after the export button is hit and re-enable it after a certain time period.
Maybe you can send the dump file through a php script. There you (again: maybe, I dont know, if it really works) can test the connection status with connection_status(). But if you send the file through a php-script, you dont need to know the status, because if the script shutdown properly, it doenst matter, if the transmission were completed, if you just want to unlock the database.
Usually a normal database dump is safe anyway. So if you let the database dump the data it contains and save it to a file, there is no reason to lock the database while someone downloads a file.
精彩评论