Skip to content Skip to sidebar Skip to footer

Redirecting Webpage To A File

Suppose a webpage that presents a table, but with an option to download it as an Excel sheet. The option is presented as a button that submits a form (containing the required param

Solution 1:

Possibility 4 (the right one): the PHP script this form points to should send a Content-type: application/vnd.ms-excelheader then the contents of the Excel file. If it's an actual file on disk, use readfile, otherwise just echo the Excel data straight out. If you want to specify a filename for download, you can also send a Content-Disposition: attachment; filename='filename' header.

Solution 2:

The script/code that handles the posting of the form (server side) can generate the Excel spreadsheet. Then instead of replying with an HTML response (or some kind of redirect) you can instead return the spreadsheet directly (with the correct HTTP headers that would be sent if the file was loaded directly).

An example in PHP:

<?php// generate spreadsheet in file$file = do_something_to_generate_spreadsheet();

// specifies it is an excel spreadsheet response
header('Content-Type: application/vnd.ms-excel'); 
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="' . $file . '"'); // filename
header('Content-Length: ' . filesize($file)); // length

readfile($file); // sends file to client?>

Solution 3:

Why have a second page at all? Make makereport.blah the xls file itself instead of another HTML document.

Solution 4:

you can use DATAURI scheme in new file to insert file in base64 mode. i'll make a script tonight.

Solution 5:

Using base64 code from webtoolkit.info and disassembling an XML/XLS file i have tried make a script to force to download a file made in the air. I make a windows object and when this will open, then insert XML/XLS code codified with base64. this method is a hack of dataURI. I have not put a name when the file download. I tried on google chrome. here an jsfiddle of file.

Thanks for your interest.

Post a Comment for "Redirecting Webpage To A File"