PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 5, 2022

[FIXED] How do I output EXCEL file directly to user without storing locally?

 October 05, 2022     ajax, php, phpexcel     No comments   

Issue

I'm using PHPExcel to generate excel files (crazy right ?) with information I get from a database, the thing is whenever I create the file it is saved in the server and then I just send a link to that file to the user so he can access it.

I doubt this is the right way of doing it, what I'm looking for is to just send the file to the browser without saving it on the server.

How do I output EXCEL file directly to user without storing locally?

Code:

$fileName  = "Request_" . $idRequest . "_Update.xls";
$objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
$objWriter->save($fileName);
$objPHPExcel = PHPExcel_IOFactory::load($fileName);


echo json_encode(array(
        "ExitCode" => 0,
        "Message" => "Success",            
        "data" => $request,
        "File" => "../reports/$fileName"
    ));

Once i receive File in the $.ajax call that receives it just add it to an anchor tag:

$.ajax({
       ...
       success : function(response){
           $('#container').append('<a href="'+response.File+'">Here is your file</a>
       }

Solution

The accepted response to this question shows how to handle a success response be offering the file for download, or a failure response handled by the js (e.g. displayed within the current html page) without mime type issues



Answered By - Mark Baker
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing