Reputation: 111
I have content that JS creates in a function and i need to print that content into my downloads folder but when i try to via ajax it will not work and is not throwing any errors
JS code
$.ajax({
url: 'includes/download.php',
type: 'post',
data: {'filename': name_file, 'content': content},
success: function(data, status) {
if(data == "ok") {
console.log('data okay');
}
},
error: function(xhr, desc, err) {
console.log(xhr);
console.log("Details: " + desc + "\nError:" + err);
}
});
PHP code
<?
$filename = $_POST['filename'];
$content = $_POST['content'];
header("Content-type: csd/");
header("Content-Disposition: attachment; filename=".$filename);
print $content;
Upvotes: 0
Views: 1075
Reputation: 2445
If you have the content in a javascript variable you don't need any ajax call to save it
var content = 'anything';
var filename = 'testFile.txt';
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:attachment/text,' + encodeURI(content);
hiddenElement.target = '_blank';
hiddenElement.download = filename;
hiddenElement.click();
The filename
variable should contain the extension as well
Upvotes: 1