jQuery文件上传添加到JSON响应

时间:2017-03-27 17:56:02

标签: jquery jquery-file-upload

我想为Blueimp的jQuery Fie Upload返回的JSON响应添加另一个值,我无法看到其他值添加到其中的位置。

我有一个函数可以返回文件的原始名称......

protected function get_orig_name($name){
        $orig_name = $name;
        return $orig_name;
} 

我想在UploadHandler.php的JSON响应中添加返回的值,但我无法研究其他值添加到数组的位置/方式。

我担心我还没有太多使用OOP PHP的经验。

这是我认为创建JSON响应的代码部分......

public function generate_response($content, $print_response = true) {
    $this->response = $content;
    if ($print_response) {
        $json = json_encode($content);
        $redirect = stripslashes($this->get_post_param('redirect'));
        if ($redirect && preg_match($this->options['redirect_allow_target'], $redirect)) {
            $this->header('Location: '.sprintf($redirect, rawurlencode($json)));
            return;
        }
        $this->head();
        if ($this->get_server_var('HTTP_CONTENT_RANGE')) {
            $files = isset($content[$this->options['param_name']]) ?
                $content[$this->options['param_name']] : null;
            if ($files && is_array($files) && is_object($files[0]) && $files[0]->size) {
                $this->header('Range: 0-'.(
                    $this->fix_integer_overflow((int)$files[0]->size) - 1
                ));
            }
        }
        $this->body($json);
    }
    return $content;
}

public function get_response () {
    return $this->response;
}

public function head() {
    $this->header('Pragma: no-cache');
    $this->header('Cache-Control: no-store, no-cache, must-revalidate');
    $this->header('Content-Disposition: inline; filename="files.json"');
    // Prevent Internet Explorer from MIME-sniffing the content-type:
    $this->header('X-Content-Type-Options: nosniff');
    if ($this->options['access_control_allow_origin']) {
        $this->send_access_control_headers();
    }
    $this->send_content_type_header();
}

public function get($print_response = true) {
    if ($print_response && $this->get_query_param('download')) {
        return $this->download();
    }
    $file_name = $this->get_file_name_param();
    if ($file_name) {
        $response = array(
            $this->get_singular_param_name() => $this->get_file_object($file_name)
        );
    } else {
        $response = array(
            $this->options['param_name'] => $this->get_file_objects()
        );
    }
    return $this->generate_response($response, $print_response);
}

public function post($print_response = true) {
    if ($this->get_query_param('_method') === 'DELETE') {
        return $this->delete($print_response);
    }
    $upload = $this->get_upload_data($this->options['param_name']);
    // Parse the Content-Disposition header, if available:
    $content_disposition_header = $this->get_server_var('HTTP_CONTENT_DISPOSITION');
    $file_name = $content_disposition_header ?
        rawurldecode(preg_replace(
            '/(^[^"]+")|("$)/',
            '',
            $content_disposition_header
        )) : null;
    // Parse the Content-Range header, which has the following form:
    // Content-Range: bytes 0-524287/2000000
    $content_range_header = $this->get_server_var('HTTP_CONTENT_RANGE');
    $content_range = $content_range_header ?
        preg_split('/[^0-9]+/', $content_range_header) : null;
    $size =  $content_range ? $content_range[3] : null;
    $files = array();
    if ($upload) {
        if (is_array($upload['tmp_name'])) {
            // param_name is an array identifier like "files[]",
            // $upload is a multi-dimensional array:
            foreach ($upload['tmp_name'] as $index => $value) {
                $files[] = $this->handle_file_upload(
                    $upload['tmp_name'][$index],
                    $file_name ? $file_name : $upload['name'][$index],
                    $size ? $size : $upload['size'][$index],
                    $upload['type'][$index],
                    $upload['error'][$index],
                    $index,
                    $content_range
                );
            }
        } else {
            // param_name is a single object identifier like "file",
            // $upload is a one-dimensional array:
            $files[] = $this->handle_file_upload(
                isset($upload['tmp_name']) ? $upload['tmp_name'] : null,
                $file_name ? $file_name : (isset($upload['name']) ?
                        $upload['name'] : null),
                $size ? $size : (isset($upload['size']) ?
                        $upload['size'] : $this->get_server_var('CONTENT_LENGTH')),
                isset($upload['type']) ?
                        $upload['type'] : $this->get_server_var('CONTENT_TYPE'),
                isset($upload['error']) ? $upload['error'] : null,
                null,
                $content_range
            );
        }
    }
    $response = array($this->options['param_name'] => $files);
    return $this->generate_response($response, $print_response);
}

0 个答案:

没有答案