Php Ajax Excel Download

1 view
Skip to first unread message

Sara Ruballos

unread,
Jan 18, 2024, 1:53:24 PM1/18/24
to alelorin

I have got a problem with getting excel file and opening download window in the browser after getting a response (in success ajax method) with that file. I have got appropriate Content-Type and Content-Disposition headers, I tried using Blob in js and I couldn't achieve what I want - simple file downloading.
I accomplished few versions of my ajax, one of them is below. I developed ajax which returns excel file which I couldn't open properly because it's corrupted (despite .xlsx extension).

php ajax excel download


Downloadhttps://t.co/yHwfZ9wmPA



I tried using "xhr.response" instead of "data" from success method arguments but it doesn't work too. I checked Response Headers in Developer Tools in Chrome and they are set properly.
The important thing - all the excel workbooks created on the server side is correct because it worked in the previous version when data was sent in URL, not in ajax post.

We were having absolutely the same trouble recently. For us it started to work when we add responseType: 'arraybuffer' to the ajax parameters. And it's better to use lib instead of manual clicking on the link because this tool revokes memory as well.

I have the following function that is pulling data from a database. The ajax call is working correctly. How can I send the tab delimited data in my success function to the user? Setting the contect type to "application/vnd.ms-excel" didn't work. The alert on success shows the correctly formatted data.

This one seems the closest to what I'm after: asp-net-mvc-downloading-excel - but I'm not sure I understand the response, and it is a couple years old now. I also came across another article (can't find it anymore) about using an iframe to handle the file download, but I'm not sure how to get this working with MVC.

My 2 cents - you don't need to store the excel as a physical file on the server - instead, store it in the (Session) Cache. Use a uniquely generated name for your Cache variable (that stores that excel file) - this will be the return of your (initial) ajax call. This way you don't have to deal with file access issues, managing (deleting) the files when not needed, etc. and, having the file in the Cache, is faster to retrieve it.

I may sound quite naive, and may attract quite a criticism, but here's how I did it,
(It doesn't involve ajax for export, but it doesn't do a full postback either )

This works for me. Make sure you return a File from your controller action with contentType as "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" and file name as e.g. "List.xlsx" which should be the same as in the AJAX success call. I have used ClosedXML NuGet package to generate the excel file.

This thread helped me create my own solution that I will share here. I was using a GET ajax request at first without issues but it got to a point where the request URL length was exceeded so I had to swith to a POST.

We have several QlikView web apps and I'm trying to pull some data from these apps to be used on a web app dashboard. The only access to the qlik apps I have is through the ajax versions. I know that I can right click on tables and export to excel but is there a way anyone knows of to do this with an external script in javascript or python perhaps? I know there are APIs for QlikSense apps to export data from mashups but I couldn't find anything for QlikView.

Everything works fine when the controller receives this post, but when it executes the SendForDownload() method in my controller, no download is actually triggered in the browser. I suspect this is because of the nature of how ajax calls are interpreted by the browser. My SendForDownload() method is almost identical to the sample:

With regards to making an ajax call, this wouldn't really work primarily because upon receiving the request, it has no idea what to do with the file. This is what you are seeing. You need to have a full post for the page to respond with the export/download.

Since all of the grid rows exist client-side after the page is initially rendered, I was hoping to just package it up and post it back to the server to be transformed into an xlsx without needing to query the database again. That's where I was trying to go with the ajax call. I'm totally okay with having the standard browser dialog pop-up (save as, open, cancel, etc).

The excelExport event allows you to reverse the cells and set the text alignment to support right-to-left (RTL) languages. To render the document in the right-to-left flow in Excel, enable the rtl option of the workbook.

The difference with its code is that i don't run a redirect to call the webscript (window.location.href = Alfresco.constants.PROXY_URI+"export/excel/?nodeRef="+file.nodeRef but i need send a json object to the java controller of the webscript.

I've performed an ajax post request to send informations through a webscript that extends the AbstractWebScript webscript type. The java controller class processes the json object and writes the response ( file excel) in the WebScriptResponse object (incomplete function).

Jspreadsheet CE is a very light, totally free, jquery plugin to embed a spreadsheet compatible with Excel in your website or application.You can create a excel-like table from a JS array, json or even a CSV file.You can use your copy and paste shortcuts to move data from excel straight to your Jspreadsheet spreadsheet and vice versa.You can easily integrate third party jquery plugins to create your own custom colums, custom editors, and customize new features to your clients.Jspreadsheet has a plenty of nice features such as key-value dropdown, CSV loading/exporting, multiple spreadsheets and much more.We have a large roadmap ahead for our jquery plugin and we are constantly improving, so don't forget to send us your ideas.

df19127ead
Reply all
Reply to author
Forward
0 new messages