我有一个JS在一个函数中创建的内容,我需要将该内容打印到我的下载文件夹中,但是当我尝试通过ajax它将无法正常工作并且不会抛出任何错误
JS代码
$.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代码
<?
$filename = $_POST['filename'];
$content = $_POST['content'];
header("Content-type: csd/");
header("Content-Disposition: attachment; filename=".$filename);
print $content;
答案 0 :(得分:1)
如果你在javascript变量中有内容,则不需要任何ajax调用来保存它
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();
filename
变量也应该包含扩展名
<强> JsFiddle 强>