2017-03-27 64 views
1

我想爲Blueimp的jQuery Fie Upload返回的JSON響應添加另一個值,並且我看不到其他值添加到其中的位置。jQuery File Upload添加到JSON響應

我有一個返回文件的原始名稱的功能...

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

,我想從UploadHandler.php JSON響應添加返回值,但我不能鍛鍊哪裏/如何其他值將被添加到數組中。

恐怕我對OOP PHP還沒有太多的經驗。

這是代碼,我覺得secion創建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); 
} 

回答

1

擺脫上傳的響應,你需要傳遞的方法get_response()在變量然後

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

你可以在json中添加你自己的值,就像我所做的一樣。