jquery - Downloading files using ajax in WordPress -


Is it possible to download the files to use WordPress Ajax? I have this function to download the attachment

  function download_attachment () {$ FILE_PATH = $ _POST ['filename']. $ File_mime = $ _POST ['MUM']; $ Data ['file_path'] = file_exists ($ file_path); Try {Header ('PGM: Public'); // Required Header ('End: 0'); // no cache header ('cache-control: re-modified, post check = 0, pre-check = 0'); Header ('Last-Modified:' .gmdate ('D, DMYH: I: S', filemtime ($ FILE_PATH)) 'GMT'.); Header ('Cash-Control: Private', Incorrect); Header ('content-type:'. $ File_mime); Header ('content-dispute: attachment; filename =' '. Bassname ($ file_path).' '' '); Header ('content-transfer-encoding: binary'); Header ('content-length:' .make file ($ file_path)); // Provide file size caption ('connection: off'); Set_time_limit (0); @readfile ("$ file_path") or die ("file not found."); } Hold (exception $ e) {$ data ['error'] = $ e-> incoming message (). "@" $ E- & gt; GetFile () '-'. $ E & gt; GetLine (); }} Echo json_encode ($ data); Die (); }  

With this function the WordPress main function is addicted to:

  add_action ('wp_ajax_download_attachment', 'download_attachment');  

and the jQuery code is:

  on data = {'function': 'download_attachment', 'file name': FILE_PATH, 'mime': Mime}; JQuery.ajax ({url: ajaxurl, type: "post", data: data, success: function (return_data, textStatus, jqXHR) {parsedData = kalimahJS.parseJSON (return_data); window.open (parsedData.url);} })  

The end result is displayed on screen 0.

Try adding the following code

  ADD_ACTION ('wp_ajax_nopriv_download_attachment ',' Download_attachment ');  

Comments