0

Here is my code:

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:

 $(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.

Noted that when I run this path: http://localhost:8000/export?filename=export.txt .. then export.txt will be downloaded.

1
  • What's in your export.txt file? Commented Nov 6, 2016 at 7:21

2 Answers 2

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(); }); }); 
Sign up to request clarification or add additional context in comments.

Comments

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

  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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.