This topic is locked
[SOLVED]

 Problem downloading a file via php

6/17/2017 9:03:11 AM
PHPRunner General questions
mbintex author

Hi,
I want a PHP button in a view to download a file with something like this:



if (file_exists($datei)) {

header('Content-Description: File Transfer');

header('Content-Type: application/octet-stream');

header('Content-Disposition: attachment; filename='.basename($datei));

flush();

readfile($datei);

exit;

}


But instead of getting a file download nothing happens. In Chrome I can get the message in console:
"Uncaught SyntaxError: Unexpected token < in JSON at position 0 ..."
in RunnerAll.js in line 414
What´s happening here?

admin 6/20/2017
mbintex author 6/21/2017

I get this from Chrome:

VM261:1 Uncaught SyntaxError: Unexpected token B in JSON at position 0

at JSON.parse (<anonymous>)

at Object.success (RunnerAll.js:414)

at j (loadfirst.js:1)

at Object.fireWith [as resolveWith] (loadfirst.js:1)

at x (loadfirst.js:1)

at XMLHttpRequest.b (loadfirst.js:1)


Following Client After Javacript isn´t processed any more. And only because I add:

$datei="files/".$data["Kurzname"].".vcf";

if (file_exists($datei)) {

header('Content-Description: File Transfer');

header('Content-Type: application/octet-stream');

header('Content-Disposition: attachment; filename='.basename($datei));

flush();

readfile($datei);

exit;

}


to the Server code part.
File $datei is written properly to files and I can fetch it from a download link written via Javascript.

admin 6/21/2017

The thing is that server code is executed behind the scene via AJAX. Any output you send from the server code will break buttons behaviour. You need to pass file name to ClientAfter event and then use something like this in ClientAfter:

location.href='filename';
mbintex author 6/21/2017

Just great.
Now my customers can download ics, vcf and so on automatically :-)