Unable to download file as PDF from browser when using LARAVEL AJAX
solution here:
success: function(blob, status, xhr) {
var ieEDGE = navigator.userAgent.match('/Edge/g');
var ie = navigator.userAgent.match('/.NET/g'); // IE 11+
var oldIE = navigator.userAgent.match('/MSIE/g');
if (ie || oldIE || ieEDGE) {
window.navigator.msSaveBlob(blob, fileName);
} else {
var fileURL = URL.createObjectURL(blob);
document.write('<iframe src="' + fileURL + '" frameborder="0" style="border:0; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%;" allowfullscreen></iframe>');
}
DomPDF does not download file for AJAX request on Laravel
Since you're making the request using AJAX, you need a way to have the browser load the resource.
There are ways to go but not limited to the following:
Return the resource URL in the response to the AJAX request then make a separate browser request to load the resource.
Make a blob of the resource response to the AJAX request then load it from the blob's URL.
This is shown in the example below.
function downloadFile(response) {
var blob = new Blob([response], {type: 'application/pdf'})
var url = URL.createObjectURL(blob);
location.assign(url);
}
$.ajax({
url: "{{ route('screen.pdfticket') }}",
method: 'POST',
data: {
id: $('#screenid').val()
}
})
.done(downloadFile);
How to open pdf from ajax response
Don't do this way,
I have a better option for you!
Don't send information via ajax. use window.location.href
example code:
$('body').on('click', '.printInvoice', function () {
var purchase_id = $(this).data("id");
window.location.href = "/purchases/print" + '/' + purchase_id;
});
Don't forget to die at end of the route.
Not able to download Pdf/Excel with JQuery AJAX get request with some data sent with get request
This works
$.ajax({
url: "http://localhost:8000/get_excel",
type: "POST",
data: {data : list_of_tables},
dataType: 'binary',
xhrFields: {
'responseType' : 'blob'
},
headers: {
'Accept': "application/json",
'Authorization': 'Bearer ' + getCookie(ACCESS_TOKEN),
},
success: function (data, textStatus, request) {
alert("success in fetching data");
let url = URL.createObjectURL(data);
let $a = $('<a />', {
'href': url,
'download': 'document.xlsx',
'text': "click"
}).hide().appendTo("body")[0].click();
// URL.revokeObjectURL(url);
},
error: function (XMLHttpRequest, data) {
console.log("error in show_history");
console.log(data);
console.log(XMLHttpRequest.status);
console.log(XMLHttpRequest.responseJSON);
}
});
Related Topics
How to Change Background Color of a Row on Checkbox Selection
React Use State to Show Otherwise Hide
How to Store Multiple Items in Local Storage
Array Map into String With Line Break - React
Copy/Paste from Excel to a Web Page
Php - Return Confirm Within PHP Issue
Converting Json Object to CSV Format in JavaScript
Bootstrap Datepicker Set Default Date Not Working
Replacing Multiple Elements of an Array While Keeping Their Position
Passing Variable from Button to Modal
Javascript Json Parsing Error: Unexpected Token ' in Json At Position 1
Warning: Failed Proptype: Invalid Prop 'Component' Supplied to 'Route'
How to Close Current Tab in a Browser Window
Domexception: Play() Can Only Be Initiated by a User Gesture
How to Validate for Only CSV File Uploads Using the Pattern Attribute Using Html5
Sum Numbers in HTML Inputs, JavaScript