[英]PHP - How to send textarea value with JavaScript AJAX and output it as .txt file for browser to download?
I have a textarea, text input and a button wich sends textarea and text input values (using AJAX post method) to PHP site. 我有一个textarea,文本输入和一个按钮,该按钮将textarea和文本输入值(使用AJAX post方法)发送到PHP站点。 Now, I want PHP to output textarea value as .txt file so that it can be download via browser.
现在,我希望PHP将textarea值输出为.txt文件,以便可以通过浏览器下载。
My PHP code looks like this: 我的PHP代码如下所示:
$text=trim($_POST['text']); // textarea value
$fileName=$_POST['fileName'].".txt"; // text input value
header("Content-Type: text/plain");
header('Content-Disposition: attachment; filename="'.$fileName.'"');
header("Content-Length: ".mb_strlen($text));
print($text);
Nothing happens, so can you help me please. 什么都没发生,所以请您能帮我。 Thanks.
谢谢。 :)
:)
Have you considered not sending the text to the server and just creating a download client-side? 您是否考虑过不将文本发送到服务器,而只是创建下载客户端?
This post has some useful suggestions you could try to create a download by bypassing the server completely Create a file in memory for user to download, not through server 这篇文章有一些有用的建议,您可以尝试通过完全绕过服务器来创建下载,而不是通过服务器在内存中创建文件供用户下载
Maybe you should clean the buffer 也许您应该清理缓冲区
while (@ob_end_clean());
ob_start();
header("Content-Type: text/plain");
header('Content-Disposition: attachment; filename="'.$fileName.'"');
header("Content-Length: ".mb_strlen($text));
print($text);
ob_end_flush();
exit();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.