-2

I recover the content from a file on a external server. This file has many lines and when I get data it is on a single line.

I understand that when I visualize it on console '\n' and '\r' appear but when I download the file I hope still have many lines without those codes.

I have a Php file which bring back the content of the file and send the response to the JavaScript

$response_ads_txt = wp_remote_get('https://www.mywebsite.com/ads.txt');
if ( is_array( $response_ads_txt ) ) {
    $body = $response_ads_txt['body'];
    echo json_encode($body);
}

And Then I have Javascript which will download the file

jQuery.post(the_ajax_script.ajaxurl, data, function(response) {
    console.log(response);

    var a = document.createElement('a');
    var data_type = 'data:text/plain;charset=utf-8';
    a.href = data_type + ', '+encodeURIComponent(response);
    a.download = 'ads.txt';
    a.click();
});

I am trying to get a file that looks like

"line1

line2

line 3..."

And not like

"line1 \n\r line2..."

Gotrek
  • 71
  • 9
  • what format you want to output? txt? html? – DaFois Jan 25 '19 at 11:13
  • This is a duplicate. Use PHP_EOL https://stackoverflow.com/questions/4195986/line-break-not-working-when-writing-to-text-file-in-php – BlueWater86 Jan 25 '19 at 11:13
  • @DaFois I want to output .txt file. – Gotrek Jan 25 '19 at 13:10
  • @BlueWater86 I tried to concatenate the content variable with PHP_EOL but it seems that it doesn't solve the problem. I precise that the goal is to allow user to download a file which is on the campany server from their plugin and having the same result that a direct download – Gotrek Jan 25 '19 at 13:10

1 Answers1

0

I solved the problem

I simply replaced

echo json_encode($body);

By :

echo $body;

Indeed there is no need to json_encode() the content as it's just text.

Gotrek
  • 71
  • 9