Jquery download file response

Jquery download file response

jquery download file response

Get the link for the file using AJAX, create an “a” tag and assign that link to the “​href”. The “a” tag must have “download” attribute. · var dlink = document. Is anybody able to tell me whats wrong? Regards. n00n. 1. Reply. Replies(4). Download File with jQuery and Web API 2.0 IHttpActionResult. I came across a var response = new HttpResponseMessage(HttpStatusCode.

Download a file via AJAX

in Using jQuery •  5 years ago  
Hi, 
I am starting with ajax and got a problem with a download I would like to make via AJAX. 
  1. $( document ).ready(function() {
  2. console.log("jQuery Version : " + $.fn.jquery)
  3. var src = $(this).attr('src');
  4. $.ajax( 'http://localhost/file-stream/response.php', {
  5. 'xhrFields' : {
  6. 'responseType' : 'blob'
  7. },
  8.    'dataType' : 'binary'
  9. })
  10. .complete(function ( xhr ) {
  11. console.log(xhr);
  12.    var $a = $( '<a />' ), url = URL.createObjectURL( xhr.response );
  13.    $a.attr({
  14.      'href' : url,
  15.      'download' : 'document.pdf'
  16.    })
  17.    .trigger( 'click' );
  18.    URL.revokeObjectURL( url );
  19.  });
  20. });
  21. });
I always get errors like this on all browsers (this ones from chrome):
  1. Uncaught InvalidStateError: Failed to read the 'responseText' property from 'XMLHttpRequest': The value is only accessible if the object's 'responseType' is '' or 'text' (was 'blob').
The downloaded file is not a part of web servers folder or in any of the vhost's folder. It is in a folder, where the webserver has permissions, but cannot use files as a link! 
This is the PHP code I use: 
  1. <?php

  2.       $file_path = '/var/www/docs/document.pdf';
  3.       $file_name = 'document.pdf';

  4. header("Content-Type: application/force-download");
  5. header("Content-type: application/pdf");
  6. header('Content-Description: File Download');
  7. header('Content-Disposition: attachment; filename=' . $file_name);
  8. header('Content-Transfer-Encoding: binary');
  9. header('Expires: 0');
  10. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  11. header('Pragma: public');
  12. header('Content-length: ' . filesize($file_path));
  13. ob_clean();
  14. flush();
  15. readfile($file_path);

  16. ?>
Is anybody able to tell me whats wrong?

Replies(4)

Leave a comment on jakecigar's reply
@jakecigar::
yeah, thats correct. My code is not working on other domains. But this is just an exemple, cause I couldn't get it running within my CMS. 
Your exemple is just downloading a png file. But I need to download doc, pdf, zip, etc.. It should download the file in the download folder. 
Leave a comment on n00n46's reply
Leave a comment on jakecigar's reply
I have a working PDF download, in this case it’s from a CORS site. I used the plugin and only had to write a few lines of code
  1. $(function() {
  2.   $("button").click(function() {
  3.     $.ajax({
  4.       url: "http://cors.jejaju.com/Snickerdoodles.pdf", // my URL
  5.       type: "GET",
  6.       dataType: 'binary',
  7.       success: function(result) {
  8.         var url = URL.createObjectURL(result);
  9.         var $a = $('<a />', {
  10.           'href': url,
  11.           'download': 'document.pdf',
  12.           'text': "click"
  13.         }).hide().appendTo("body")[0].click();

  14.         // URL.revokeObjectURL(url);
  15.       }
  16.     });
  17.   });
  18. });
Leave a comment on jakecigar's reply
Reply to n00n46's question
{"z47173392":[14737000005993546,14737000005993567],"z2950240":[14737000005994920,14737000005999092,14737000005999144]}
Источник: [https://torrent-igruha.org/3551-portal.html]

Jquery download file response

0 thoughts to “Jquery download file response”

Leave a Reply

Your email address will not be published. Required fields are marked *