tatty27
tatty27

Reputation: 1554

jQuery File Upload adding to JSON response

I would like to add another value to the JSON response returned by Blueimp's jQuery Fie Upload and I can't see where the other values are added to it.

I have a function which returns the file's original name...

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

and I would like to add the returned value in the JSON response from UploadHandler.php but I can't workout where/how the other values are added to the array.

I'm afraid I don't have much experience with OOP PHP as yet.

This is the secion of code I think creates the JSON response...

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

Upvotes: 1

Views: 1059

Answers (1)

eaponz
eaponz

Reputation: 604

to get the response from the uploader, you need to pass the method get_response() in your variable then

$uploader = new UploadHandler();
$response = $uploader->get_response(); // returned as array
// if your param_name is 'file'
$response['file'][0]->custom_property = 'my address'

you can add now your own values in the json like what I did.

Upvotes: 1

Related Questions