принудительная загрузка xlsx из ajax-ответа не работает


У меня небольшая проблема с загрузкой моего xlsx-файла. Я отправляю запрос на файл через jquery Ajax, и на серверной части данные правильно собраны и собраны в xlsx-файл. Так что теперь на обратном пути к интерфейсу я настраиваю все заголовки, готовясь к принудительной загрузке файла, но загрузка никогда не начинается.

Это заголовки ответов на мой запрос:

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

Имхо загрузка должна начаться немедленно, но ничего происходит.

РЕДАКТИРОВАТЬ: До сих пор я использовал форму отправки, но объем данных действительно велик, поэтому время, необходимое для сборки файла, также очень велико, а иногда и пару минут или даже час, так что это было уже невозможно.

Поэтому я создал java-задание для создания файла и запустил фрагмент ajax, который запрашивает завершение каждую секунду или около того.

Итак, вот мой код. Внешний интерфейс: Это называется нажатием кнопки

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();
    }

Запрос Ajax на отправить:

$.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>
    }
});

Статус проверки:

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();
        }
    });
}

Серверная часть - 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 ++++

Серверная часть - java - Здесь собран файл:

(...)
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);

Макет/excel:

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

ПРАВКА 2: Поэтому я попытался открыть новое окно при успешном использовании данных, и я мог бы начать загрузку, но файл больше не является допустимым файлом xlsx.

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');
    }

Есть идеи?

Author: Jan Wedel, 2015-11-23

2 answers

После долгих исследований я нашел этот сайт, и суть его утверждения заключается в том, что jquery ajax не поддерживает получение двоичных данных, но предоставляет решение для реализации простого запроса xhr, которое поддерживает передачу больших двоичных объектов. сайт: http://www.henryalgus.com/reading-binary-files-using-jquery-ajax/

 0
Author: Jan Wedel, 2015-11-24 09:26:13

Чтобы расширить мой комментарий, вместо того, чтобы пытаться отправить обратно двоичные данные через ajax, просто сохраните их во временный файл, отправьте ссылку на файл обратно в js. Получив ссылку на файл, просто установите window.location.href, чтобы указать конечную точку чтения файлов, передавая ссылку на файл. Я делал это несколько раз, и это прекрасно работает даже в древних браузерах:

$('#start').click(function(){
    $.post('/createfile.php', {some:data}, function(response){
        if(response.started){
            pollFile(response.fileId);
        }
    });
);
function pollFile(fileId){
    $.get('/filestatus.php?fileid=' + fileId, function(response){
        if(response.fileCompleted){
            window.location.href = '/downloadfile.php?fileid=' + fileId;
        }else{
            setTimeout('pollFile', 5000, fileId);
        }
    });
}

//createfile.php    
$fileId = uniqid();
SomePersistentStorage::addJob($fileID);
//start file job here, code should run in a seperate process/thread, so
//either use a job queue system, use shell_exec or make an http request,
//then once job is queued/started:
header('Content-Type: application/json');
echo json_encode(['started'=>true, 'fileId'=>$fileId]);

//processjob.php - the file that does the work, could be your java
//program for example, just using php here for consistency
//after file is done
file_put_contents($filepath, $data);
SomePersistentStorage::updateJob($fileID, true);

//filestatus.php
$fileId = $_GET['fileid'];
header('Content-Type: application/json');
echo json_encode(['fileCompleted'=>SomePersistentStorage::isJobCompleted($fileID)]);

//downloadfile.php
$fileId = $_GET['fileid'];
$filepath = 'tmp/' . $fileId . '.tmp';
//correct headers here, then
readfile($filepath);
unlink($filepath);

Если вы не хотите немедленно удалять файл, то вы можете просто запустить cron для удаления файлов в определенной папке, которые старше х.

 0
Author: Steve, 2015-11-24 12:05:27