admin管理员组文章数量:1326081
I want to save a value to a txt-file and download it to the user.
Right now, the value is being printed into the txt-file correctly, but the readfile function is not triggered, thus no downloading begins.
The php, this code is located on the same page as the ajax call.
<?php
if (isset($_POST['data']))
{
$handle = fopen("file.txt", "w");
fwrite($handle, $_POST['data']);
fclose($handle);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename('file.txt'));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('file.txt'));
readfile('file.txt');
exit;
}
?>
The javascript, there is no url because the data is to be sent to the current page.
function exportColors() {
var exportData = this.dataset.id;
$.ajax({
type: "POST",
data: {data: exportData},
success: function (data) {
console.log(data);
}
});
}
I want to save a value to a txt-file and download it to the user.
Right now, the value is being printed into the txt-file correctly, but the readfile function is not triggered, thus no downloading begins.
The php, this code is located on the same page as the ajax call.
<?php
if (isset($_POST['data']))
{
$handle = fopen("file.txt", "w");
fwrite($handle, $_POST['data']);
fclose($handle);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename('file.txt'));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('file.txt'));
readfile('file.txt');
exit;
}
?>
The javascript, there is no url because the data is to be sent to the current page.
function exportColors() {
var exportData = this.dataset.id;
$.ajax({
type: "POST",
data: {data: exportData},
success: function (data) {
console.log(data);
}
});
}
Share
Improve this question
asked Dec 15, 2015 at 8:39
JoelJoel
4604 silver badges17 bronze badges
1
- Are you getting any errors? Turn on error reporting and check what your ajax returns in firebug (firefox) or something else... – Naruto Commented Dec 15, 2015 at 8:44
5 Answers
Reset to default 4You need to separate the functionality, that is, post the data to PHP first, save the content of the text file and then, in a second request, let the user download the file. So one (skeleton) approach would be:
JS File:
function exportColors() {
var exportData = this.dataset.id;
$.ajax({
type: "POST",
data: {data: exportData},
success: function (data) {
// redirect or print out a link
}
});
}
PHP File for the first request (saving the content):
<?php
if (isset($_POST['data'])) {
$handle = fopen("file.txt", "w");
fwrite($handle, $_POST['data']);
fclose($handle);
// give back some unique id or the unique filepath
}
?>
PHP File for the second request (be it through clicking on a link or after having been redirected):
// essentially everything that outputs your file
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename('file.txt'));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('file.txt'));
readfile('file.txt');
exit;
Comments: Either give back a unique filepath or via a handle through a database (more secure, but more plex as well). Besides, why should the user download the unchanged data he has formerly submitted? Is there more to it than meets the eye?
Ok I'm going out on a limb so take this with a grain of salt.
You said this request is going to the current page? Does that mean you've excluded (for the sake of brevity) HTML content from the PHP file you're showing us? If you have ANY content above your opening <?php
tag, then any call to header
will fail with an error (because headers must e before the body of a request). This would cause the script to terminate and prevent the readfile
line from ever being reached.
Are your headers being sent? Do you see anything in the error log? If the answers to these are yes and no respectively then I'm wrong and you can ignore this.
Due to security reasons, ajax won't be able to download/save the file to the client's system because of java-script constraints.
Instead you can use
jquery.fileDownload.js
Please have a look at http://jqueryfiledownload.apphb./
Hope this helps.
you can't
beacuse javascript is not able to write on user file system.
see answer
Download a file by jQuery.Ajax
explain problem very well
The following Ajax generates a new file on the server at runtime AND downloads it to the client machine's /downloads directory with a single click. It uses Javascript to download the file, not PHP readfile().
await $.ajax("generateFile.php", {
type: "POST",
data: {
param_1: param_1,
param_2: param_2
},
success: function(data){
var a = $("<a>")
.attr("href", "path/filename_on_server.txt")
.attr("download", "filename_on_client.txt")
.appendTo("body");
a[0].click();
a.remove();
},
error : function(data) {
alert("ajax error, json: " + data);
}
});
本文标签: javascriptDownload file with ajax and phpreadfile not workingStack Overflow
版权声明:本文标题:javascript - Download file with ajax and php - readfile not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742195219a2430954.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论