douwei6478 2015-11-23 13:45 采纳率: 100%
浏览 90
已采纳

强制下载xlsx从ajax响应无效

I have this little problem with downloading my xlsx-file. I am sending my request for the file over jquery Ajax and on the backend the data is correctly collected and assembled to a xlsx-file. So now on its way back to the frontend i am setting all the headers in preparation to force download the file, but the download never starts.

These are the response headers of my request:

Connection      Keep-Alive
Content-Disposition attachment; filename="export.xlsx"
Content-Length  346420
Content-Type    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Date            Mon, 23 Nov 2015 13:23:30 GMT
Keep-Alive      timeout=5, max=91
Server          Apache/2.4.16 (Win32) OpenSSL/1.0.1p PHP/5.6.12
Set-Cookie      <cookiesettings>
content-transfer-encoding   binary
x-powered-by    PHP/5.6.12

imho the download should start immediately, but nothing happens.

EDIT: Until now I used a form submit, but the data amount is really big so the time which is needed to assemble the file is also really long and sometimes a couple of minutes or even an hour, so this was no longer possible.

So I built a java-job to build the file and startet an ajax snippet which asks for completion every second or so.

So here is my Code. Frontend: This is called on button-click

download: function (type, maximum) {
        var
            self = this,
            oParams = this.oTable.oApi._fnAjaxParameters(this.oTable.fnSettings()),
            aoPost = [
                { 'name': 'exportType', 'value': type },
                { 'name': 'exportMax', 'value': maximum },
                { 'name': 'handleId', 'value': self.options.handleId }
            ],
            nIFrame, nContentWindow, nForm, nInput, i
        ;

        // Call a self made function to get extra search parameters
        // without call an data update AJAX call.
        self.oTable.fnSettings().addAdditionalSearchData(oParams);

        // Create an IFrame to do the request
        nIFrame = document.createElement('iframe');
        nIFrame.setAttribute('id', 'RemotingIFrame');
        nIFrame.style.border = '0px';
        nIFrame.style.width = '0px';
        nIFrame.style.height = '0px';

        document.body.appendChild(nIFrame);
        nContentWindow = nIFrame.contentWindow;
        nContentWindow.document.open();
        nContentWindow.document.close();

        nForm = nContentWindow.document.createElement('form');
        nForm.className = 'export-table';
        nForm.setAttribute('method', 'post');

        // Add POST data.
        var formData = {};
        for (i = 0; i < aoPost.length; i++) {
            nInput = nContentWindow.document.createElement('input');
            nInput.setAttribute('name', aoPost[ i ].name);
            nInput.setAttribute('type', 'text');
            nInput.value = aoPost[ i ].value;
            nForm.appendChild(nInput);
            formData[aoPost[ i ].name] = aoPost[ i ].value;
        }

        // Add dataTables POST.
        for (i = 0; i < oParams.length; i++) {
            nInput = nContentWindow.document.createElement('input');
            nInput.setAttribute('name', oParams[ i ].name);
            nInput.setAttribute('type', 'text');
            nInput.value = oParams[ i ].value;
            nForm.appendChild(nInput);
            formData[oParams[ i ].name] = oParams[ i ].value;
        }

        nForm.setAttribute('action', '/service/exportTableData');

        // Add the form and the iFrame.
        nContentWindow.document.body.appendChild(nForm);

        // Send the request.
        //nForm.submit();


        // Send the request.
        var form = $(nContentWindow.document.body).find('form.export-table');

        var jobId = 0;

        form.ajaxForm(
            {
                'showMessagesOnSuccess': false
            },
            {
                'getData': function () {
                    return formData;
            }
            }
        ).data('ajaxForm').submit();
    }

The Ajax request on submit:

$.ajax({
    type: 'POST',
    url: self.handler.getServiceUrl(),
    timeout: GLOBALS.AJAX_REQUEST_TIMEOUT,
    cache: false,
    data: (<get the Data>)
    ,
    success: function (response) {
        if (response.success === true) {
            // Check if we have to wait for a result.
            if (response.jobId !== undefined && response.jobId !== 0) {
                self.checkJobStatus(response.jobId);
            } else {
                <success - show some messages>
            }
        } else {
            self.handler.error(response);
        }
    },
    error: function () {
        <Show error Message>
    }
});

The CheckJobStatus:

checkJobStatus: function (jobId) {
    var self = this;
    $.ajax({
        type: 'POST',
        timeout: GLOBALS.AJAX_REQUEST_TIMEOUT,
        cache: false,
        data: { 'jobId': jobId },
        url: self.handler.getServiceUrl(),
        success: function (response) {
            if(response !== null && response.data !== undefined) {
                if (response.data.isFinished === true) {
                    if (response.success === true) {
                        // Check if we have to wait for a result.
                        self.handler.success(response);
                    } else {
                        self.handler.error(response);
                    }
                } else if (response.success === true && response.data !== null) {
                    setTimeout(
                        function () {
                            self.checkJobStatus(jobId);
                        },
                        500
                    );
                } else {
                    Helper.logFrontendError();
                }
            } else if (response !== null && response.success === true) {
                setTimeout(
                    function () {
                        self.checkJobStatus(jobId);
                    },
                    1000
                );
            } else {
                    Helper.logFrontendError();
            }
        },
        error: function (response) {
                Helper.logFrontendError();
        }
    });
}

Backend - php:

(...)
if ($action == 'exportTableData' || $action == 'exportChartData') {
            $responseData = $service->execute();
            if(isset($responseData->data['contentType']) && $responseData->data['contentType'] != null && isset($responseData->data['data'])) {
                $this->sendTextData($responseData->data['contentType'], $responseData->data['data']);
            } else {
                $this->sendJsonData($responseData);
            }
        } else {
            $this->sendJsonData($service->execute());
        }
(...)


private function sendTextData($contentType, $data) {
    $this->set('filename', 'export.xlsx');
    $this->set('data', $data);
    $this->response->type($contentType);
    $this->render('/Layouts/excel', 'excel');
}


(...)
$handlerResult = new HandlerResult();

    if($dataServiceResult == null) {
        $service = new DataService();
            $dataServiceResult = $service->exportTableData(
                    $controller->Auth->User('id'),
                    json_encode($request->data),
                    null
            );
    } else {
        if ($dataServiceResult->header->resultKey == 0) {
            $handlerResult->wsData['data'] = $dataServiceResult->data;
            $handlerResult->wsData['contentType'] = $dataServiceResult->contentType;
        }
    }
    $handlerResult->wsResultHeader = $dataServiceResult->header;
    return $handlerResult; // ++++ this result returns to the first codeblock in this section ++++

Backend - java - This is where the File is assembled:

(...)
if (jobId > 0) {
            FrontendJobStatus status = FrontendJobQueue.getJobStatus(context.userId, jobId);
            this.result = (WSExportTableDataResult) status.getResult();
            logger.info((this.result.data == null) ? "ByteArray is EMPTY" : "ByteArray is NOT EMPTY");
        } else {
            this.jobId = FrontendJobQueue.addJob(this.context.userId, new ExportTableDataJob(this.context, this.postData));
            this.result.header.jobId = this.jobId;
        }
(...)

The Jop:
<Workbook assembly>
ByteArrayOutputStream out = new ByteArrayOutputStream();
wb.write(out);
this.result.data = out.toByteArray();
        this.result.contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        // this.result.contentType = "application/vnd.ms-excel";

        this.result.setResultHeader(APIConstants.RESULT_SUCCESS);

Layout/excel:

<?php
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Transfer-Encoding: binary');
ob_clean();
echo $data;

EDIT 2: So I tried to open a new window on success with the Data, and i could start the download, but the file ist no valid xlsx File anymore.

var reader = new FileReader();
    var blob = new Blob([response.responseText], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
    reader.readAsDataURL(blob);

    reader.onloadend = function (e) {
        window.open(reader.result, 'Excel', 'width=20,height=10,toolbar=0,menubar=0,scrollbars=no', '_blank');
    }

Any Ideas?

  • 写回答

2条回答 默认 最新

  • dqoeghe9452 2015-11-24 09:26
    关注

    After a lot of research i found this site and the essence of its statment is that jquery ajax does not support receiving binary data, but provides a solution for implementing plain xhr request which support blob transfer. The Site: http://www.henryalgus.com/reading-binary-files-using-jquery-ajax/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)