ZF2文件上传进度不是为我工作(ZF2 File Upload progress not worki

2019-10-21 17:38发布

我想上传我们的网页上的一个文件。 它multiupload,我需要跟踪进度为好。 问题是我进步的细节XHR请求返回“没有进行上传”的消息。

因此,这里的代码:

JS:

$file.on('change', function(e) {
        e.preventDefault();
        $('#upload-button').prop('disabled', 'disabled');
        hideErrors();
        hideProgress();
        if ($file.val() == '') {
            showErrors('No file(s) selected');
            return;
        }
        //$.fn.ajaxSubmit.debug = true;
        $(this).closest("form").ajaxSubmit({
            url: '/task/upload-attachments',
            target: '#output',
            success: function (response, statusText, xhr, $form) {
                $('#upload-button').removeProp('disabled');
                if (response.status == 'success') {
                    var attachmentNames = $attachmentNames.val().split('###');
                    $.each(response.attachments, function(key, attachment) {
                        attachmentNames.push(attachment);
                        $('#attachments-list').append(
                            '<li class="attachment-item attachment-temp">' +
                                '<div class="btn-group">' +
                                    '<a href="#" class="btn btn-sm btn-default dropdown-toggle attachment-btn" data-toggle="dropdown" aria-expanded="false">' +
                                        '<span class="glyphicon glyphicon-paperclip"></span> ' + attachment + ' <span class="caret"></span>' +
                                    '</a>' +
                                    '<ul class="dropdown-menu" role="menu">' +
                                        '<li>' +
                                            '<a href="#" class="delete-attachment-btn">' +
                                                '<span class="glyphicon glyphicon-remove-circle text-danger"></span> Delete' +
                                            '</a>' +
                                        '</li>' +
                                    '</ul>' +
                                '</div>' +
                            '</li>'
                        )
                    });
                    $attachmentNames.val(attachmentNames.join('###'));
                }
                $file.val('');
            },
            error: function(a, b, c) {
                $('#upload-button').removeProp('disabled');
                $file.val('');
            }
        });
        startProgress();
    });

服务器端处理(PHP):

public function sessionProgressAction()
    {
        $id = $this->params()->fromQuery('id', null);
        $progress = new ProgressBar\Upload\SessionProgress();
        $view = new JsonModel([
            'id'     => $id,
            'status' => $progress->getProgress($id),
        ]);
        return $view;
    }

    public function uploadAttachmentsAction()
    {
        $form = new TaskUploadForm('task-files');
        $request = $this->getRequest();

        $files = [];
        $attachmentNames = [];
        if ($request->isPost()) {
            // Postback
            $data = array_merge_recursive(
                $request->getPost()->toArray(),
                $request->getFiles()->toArray()
            );
            $form->setData($data);
            if ($form->isValid()) {
                $files = $form->getData()['file'];
                foreach ($files as $file) {
                    $attachmentNames[] = pathinfo($file['tmp_name'], PATHINFO_BASENAME);
                }
            }
        }

        if (count($files)) {
            return new JsonModel([
                'status' => 'success',
                'msg' => 'Attachment successfully added.',
                'attachments' => $attachmentNames
            ]);
        } else {
            return new JsonModel([
                'status' => 'error',
                'msg' => 'Failed to upload attachments.'
            ]);
        }
    }

Serverside集团Form类:

public function addElements()
    {
        // File Input
        $file = new Element\File('file');
        $file
            ->setLabel('File Input')
            ->setAttributes([
                'id'       => 'file',
                'multiple' => true,
            ]);
        $this->add($file);
        // Progress ID hidden input is only added with a view helper,
        // not as an element to the form.
    }
    public function createInputFilter()
    {
        $inputFilter = new InputFilter\InputFilter();
        // File Input
        $file = new InputFilter\FileInput('file');
        $file->setRequired(true);
        $file->getFilterChain()->attachByName(
            'filerenameupload',
            [
                'target'          => Constants::UPLOAD_TMP . 'attachment',
                'overwrite'       => true,
                'randomize'       => true,
                'use_upload_name' => true
            ]
        );
        // Allowed extensions
        $file->getValidatorChain()->attachByName(
            'fileextension',
            [
                'doc', 'docx', 'xls', 'xlsx', 'pdf', 'csv', 'png', 'jpg', 'gif', 'ods', 'odt', 'ott', 'txt'
            ]
        );
        $inputFilter->add($file);
        return $inputFilter;
    }

我检查了PHP的配置,并设置相应的选项:

session.upload_progress.enabled =开session.upload_progress.freq = “1%” session.upload_progress.min_freq = “1”

而且还关闭上nginx的代理缓存。

任何人能帮助我吗? 我们将不胜感激

文章来源: ZF2 File Upload progress not working for me