Here is my code:
这是我的代码:
PHP:
PHP:
public function export(Request $request){
$file = "export.txt";
if(isset($_POST["type"])){
file_put_contents("$file",$_POST["text"]);
}
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: text/plain'); // the appropriate header type for txt file
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
}
JS:
JS:
$(document).on('click', '#export', function () {
var names = ['علی','فرید'];
var namess = names.join('\n');
$.ajax({
type: "post",
url: "/export",
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
data: {
type: "save",
text: namess
},
dataType: "json",
success: function(){
var href = '/export?filename=export.txt';
window.location = href;
},
error: function(){
alert('wrong');
}
});
})
Always error
part executes. I mean it always alert wrong
. How can I fix it? All I'm trying to do it making a .txt
download file.
总是错误执行的一部分。我的意思是它总是警告错误。我怎样才能修好它呢?我要做的就是制作一个。txt下载文件。
Noted that when I run this path: http://localhost:8000/export?filename=export.txt
.. then export.txt
will be downloaded.
注意,在运行此路径时:http://localhost:8000/export?filename=export。txt . .然后出口。txt将下载。
2 个解决方案
#1
1
You can download using this code:
你可使用此程式下载:
window.location="export?filename=export.txt";
If you want to post data :
如果你想发布数据:
$('<form action="comments.php" method="POST"/>')
.append($('<input type="hidden" name="type" value="save">'))
.append($('<input type="hidden" name="text" value="'+ namess +'">'))
.appendTo($(document.body)) //it has to be added somewhere into the <body>
.submit();
Full code:
完整的代码:
$(document).on('click', '#export', function () {
var names = ['علی','فرید'];
var namess = names.join('\n');
$('<form action="export?filename=export.txt" method="POST"/>')
.append($('<input type="hidden" name="type" value="save">'))
.append($('<input type="hidden" name="text" value="'+ namess +'">'))
.appendTo($(document.body)) //it has to be added somewhere into the <body>
.submit();
});
});
#2
1
Basically Ajax is not usually used for file download however you can tune to make it feel and that is what you have done the only thing is when the request is successful us a "document.location" function from javascript to popup a new window for downloading a file. Besides for the error you are getting, try debugging your PHP code by playing around with your PHP header by starting with most important PHP Headers such as
基本上Ajax通常不用于文件下载,但是您可以对其进行调优,这就是您所做的,惟一的事情是当请求成功时,我们将得到一个“文档”。定位功能从javascript弹出一个新窗口下载文件。除了得到的错误之外,还可以从最重要的PHP头(如PHP头)开始调试PHP代码
header('Content-Type: text/plain');
header('Content-Disposition: attachment;filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
ob_clean();
flush();
readfile($fileName);
exit;
check you code works with minimal information if it works then start adding more header one at a time, this helps in resolving problem with minimal information. Hope this helps.
检查您的代码是否使用了最少的信息,如果它有效,然后开始每次添加更多的头信息,这有助于用最少的信息解决问题。希望这个有帮助。
#1
1
You can download using this code:
你可使用此程式下载:
window.location="export?filename=export.txt";
If you want to post data :
如果你想发布数据:
$('<form action="comments.php" method="POST"/>')
.append($('<input type="hidden" name="type" value="save">'))
.append($('<input type="hidden" name="text" value="'+ namess +'">'))
.appendTo($(document.body)) //it has to be added somewhere into the <body>
.submit();
Full code:
完整的代码:
$(document).on('click', '#export', function () {
var names = ['علی','فرید'];
var namess = names.join('\n');
$('<form action="export?filename=export.txt" method="POST"/>')
.append($('<input type="hidden" name="type" value="save">'))
.append($('<input type="hidden" name="text" value="'+ namess +'">'))
.appendTo($(document.body)) //it has to be added somewhere into the <body>
.submit();
});
});
#2
1
Basically Ajax is not usually used for file download however you can tune to make it feel and that is what you have done the only thing is when the request is successful us a "document.location" function from javascript to popup a new window for downloading a file. Besides for the error you are getting, try debugging your PHP code by playing around with your PHP header by starting with most important PHP Headers such as
基本上Ajax通常不用于文件下载,但是您可以对其进行调优,这就是您所做的,惟一的事情是当请求成功时,我们将得到一个“文档”。定位功能从javascript弹出一个新窗口下载文件。除了得到的错误之外,还可以从最重要的PHP头(如PHP头)开始调试PHP代码
header('Content-Type: text/plain');
header('Content-Disposition: attachment;filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
ob_clean();
flush();
readfile($fileName);
exit;
check you code works with minimal information if it works then start adding more header one at a time, this helps in resolving problem with minimal information. Hope this helps.
检查您的代码是否使用了最少的信息,如果它有效,然后开始每次添加更多的头信息,这有助于用最少的信息解决问题。希望这个有帮助。