[Solved] File Download From Ajaxsubmit

I am unable to download a file using an ajaxsubmit button


echo CHtml::ajaxSubmitButton('Export to Excel',$this->createUrl('/agent/Payment/ExcelExport'))

In actionExcelExport, i am using CFile extension to download file




$file=Yii::app()->file->set($path, true); //using CFile extension

$file->send('report.xlsx', false);



I believe there is not any issue with the CFile extension, because If i directly call action /agent/Payment/ExcelExport, then the file is downloaded successfully.

Please guide where is the issue.

regards,

Shahzad

Do you send some post/get data to this url, using form under which you have this ajaxSubmitButton?

Press F12 or open Firebug and in Console see what is the request and what is the answer.

In chrome i have following in header

Request URL:http://localhost/mts/index.php?r=agent/Payment/ExcelExport

Request Method:POST

Status Code:200 OK

Request Headersview source

Accept:/

Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3

Accept-Encoding:gzip,deflate,sdch

Accept-Language:en-GB,en-US;q=0.8,en;q=0.6

Connection:keep-alive

Content-Length:292

Content-Type:application/x-www-form-urlencoded

Cookie:PHPSESSID=tqp3ujt1e47km2kgnrspfv8bn7

Host:localhost

Origin:http://localhost

Referer:http://localhost/mts/index.php?r=agent/Payment/admin

User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22

X-Requested-With:XMLHttpRequest

Query String Parametersview sourceview URL encoded

r:agent/Payment/ExcelExport

Form Dataview sourceview URL encoded

PaymentSearch[agent_id]:

PaymentSearch[insert_date]:

PaymentSearch[pin_number]:

PaymentSearch[customer_id]:

PaymentSearch[beneficiary_id]:

PaymentSearch[company_id]:

PaymentSearch[amount_due_lc]:

PaymentSearch[amount_due_fc]:

PaymentSearch[payment_status]:

Response Headersview source

Cache-Control:private

Connection:Keep-Alive

Content-Disposition:attachment;filename="report.xlsx"

Content-Length:0

Content-Transfer-Encoding:binary

Content-Type:application/octet-stream

Date:Sat, 30 Mar 2013 05:59:52 GMT

Expires:Mon, 26 Jul 1997 05:00:00 GMT

Keep-Alive:timeout=5, max=100

Pragma:private

Server:Apache/2.2.22 (Win32) PHP/5.3.13

X-Powered-By:PHP/5.3.13

X-Sendfile:C:\wamp\www\mts\files\temp\report3.xlsx

Nothing is shown in preview and response tabs.

Is this data




PaymentSearch[agent_id]:

PaymentSearch[insert_date]:

PaymentSearch[pin_number]:

PaymentSearch[customer_id]:

PaymentSearch[beneficiary_id]:

PaymentSearch[company_id]:

PaymentSearch[amount_due_lc]:

PaymentSearch[amount_due_fc]:

PaymentSearch[payment_status]:



indeed empty ? Or you just cleared it in this example? Because if you don’t send anything in post, then solution is very easy. Instead of triggering form submission under given url and trying to do something with response that contain binary data, it’s much easier to just execute this one line of code, when the button is clicked:


window.location = "http://localhost/mts/index.php?r=agent/Payment/ExcelExport"

.

Ok, i have done it without ajaxSubmit, used simple submit button.