Hi,
I am starting with ajax and got a problem with a download I would like to make via AJAX.
- $( document ).ready(function() {
- console.log("jQuery Version : " + $.fn.jquery)
- $('.documentIcon').click(function(){
- var src = $(this).attr('src');
- $.ajax( 'http://localhost/file-stream/response.php', {
- 'xhrFields' : {
- 'responseType' : 'blob'
- },
- 'dataType' : 'binary'
- })
- .complete(function ( xhr ) {
- console.log(xhr);
- var $a = $( '<a />' ), url = URL.createObjectURL( xhr.response );
- $a.attr({
- 'href' : url,
- 'download' : 'document.pdf'
- })
- .trigger( 'click' );
- URL.revokeObjectURL( url );
- });
- });
- });
I always get errors like this on all browsers (this ones from chrome):
- 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:
- <?php
-
- $file_path = '/var/www/docs/document.pdf';
- $file_name = 'document.pdf';
-
- header("Content-Type: application/force-download");
- header("Content-type: application/pdf");
- header('Content-Description: File Download');
- header('Content-Disposition: attachment; filename=' . $file_name);
- header('Content-Transfer-Encoding: binary');
- header('Expires: 0');
- header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
- header('Pragma: public');
- header('Content-length: ' . filesize($file_path));
- ob_clean();
- flush();
- readfile($file_path);
-
- ?>
Is anybody able to tell me whats wrong?
Regards
n00n