Friday 15 July 2011

php - POST without using forms, then download file -



php - POST without using forms, then download file -

i'm using ajax post, ajax won't trigger file download. need post without using forms, because i'm using values many elements on page, made javascript function values , create array post php target file. how can post these values , trigger download file, without using form submit, collecting elements values , sending target php page?

javascript parses content , creates array of values, , post php:

['jordan','22','male','other info']

php file uses array insert these values docx (document), returns download url, note file isn't kept in server. here's ajax post answer:

access-control-allow-origin:* cache-control:max-age=0, no-cache, no-store, must-revalidate connection:keep-alive content-description:file transfer content-disposition:attachment; filename="modelos/calculos/tempo_contribuicao_2014-11-12.docx" content-length:18943 content-transfer-encoding:binary content-type:application/vnd.openxmlformats-officedocument.wordprocessingml.document date:wed, 12 nov 2014 15:57:39 gmt expires:wed, 11 jan 1984 05:00:00 gmt keep-alive:timeout=15, max=98 pragma:no-cache server:apache/2.0.63 (freebsd) php/5.2.6 suhosin-patch vary:user-agent x-powered-by:php/5.2.6

you can accomplish same using:

step 1: first, send ajax request post parameters server

step 2: generate file on server using php & homecoming filename

step 3: in ajax success callback, utilize location.href='<path_to_php_to_download_file>?filename=fname' //<-- fname filename returned in ajax success callback

step 4: php file download file & remove after sending on browser.

php ajax

No comments:

Post a Comment