2
votes

I am using ajax to call a php file passing along the data I want written to an Excel file. I do not want to save the Excel file on the server, but want to use the $objWriter->save('php://output'); (part of PHPExcel) to open it in the browser.

If I copy the ajax script call into my browser (outside my web application), the browser creates the file and the browser gives the user the option to open/save it.

But...when I run it through the ajax call in my web application, nothing shows up.

I've seen many posts where people had similar issues, but they do not really match what I am trying to do. Most seemed to actually create the file on the server and then return the file name back as the result to the ajax call and then open that file. Since I am not creating a file on the server, that won't work for me.

Is there a way to accomplish this with ajax, php, and PHPExcel? Is there a way to open the file as part of the $objWriter->save('php://output') statement?

1
It would probably be helpful if you posted the (relevant) code that you're using. - Patrick Q
Your Ajax call has to handled a streamed binary response if you want to do this through Ajax; it's a lot easier to simply use a link for the download - Mark Baker
By using a link wouldn't that mean I would have to store the file on the server first and then let the person click the link to get to the file (unless I am misunderstanding what you mean)? Since multiple people could be using this application and creating quite a few of the Excel files, I didn't want to actually create the files on the server - otherwise I'd have to work on some sort of purging routine to clean up files. - user3861284

1 Answers

0
votes

Well I got around the issue....instead of calling the script using the ajax call, I did a window.open and used the script call I was using for the ajax call as the input. So what it does is temporarily open a window, which in return calls the php script (passing all parameters). Once the Excel file is created, the window closes but the browser file shows for the user. So except for a window temporarily flashing, the end result is what was originally desired.