diff --git a/core/lib/Drupal/Core/Ajax/AjaxCommandInterface.php b/core/lib/Drupal/Core/Ajax/AjaxCommandInterface.php
new file mode 100644
index 0000000..7b4ebfe
--- /dev/null
+++ b/core/lib/Drupal/Core/Ajax/AjaxCommandInterface.php
@@ -0,0 +1,11 @@
+<?php
+namespace Drupal\Core\Ajax;
+
+interface AjaxCommandInterface {
+
+  /**
+   * Return an array that will be run through json_encode and sent to the
+   * client.
+   */
+  public function render();
+}
diff --git a/core/lib/Drupal/Core/Ajax/AjaxCommandReplace.php b/core/lib/Drupal/Core/Ajax/AjaxCommandReplace.php
new file mode 100644
index 0000000..14194de
--- /dev/null
+++ b/core/lib/Drupal/Core/Ajax/AjaxCommandReplace.php
@@ -0,0 +1,29 @@
+<?php
+namespace Drupal\Core\Ajax;
+
+use Drupal\Core\Ajax\AjaxCommandInterface;
+
+class AjaxCommandReplace implements AjaxCommandInterface {
+
+  protected $selector;
+  protected $html;
+  protected $settings;
+
+  public function __construct($selector, $html, $settings = NULL) {
+    $this->selector = $selector;
+    $this->html = $html;
+    $this->settings = $settings;
+  }
+
+  public function render() {
+
+    return array(
+        'command' => 'insert',
+        'method' => 'replaceWith',
+        'selector' => $this->selector,
+        'data' => $this->html,
+        'settings' => $this->settings,
+    );
+  }
+
+}
\ No newline at end of file
diff --git a/core/lib/Drupal/Core/Ajax/AjaxResponse.php b/core/lib/Drupal/Core/Ajax/AjaxResponse.php
new file mode 100644
index 0000000..79f9b7b
--- /dev/null
+++ b/core/lib/Drupal/Core/Ajax/AjaxResponse.php
@@ -0,0 +1,33 @@
+<?php
+
+namespace Drupal\Core\Ajax;
+
+use Symfony\Component\HttpFoundation\JsonResponse;
+
+
+class AjaxResponse extends JsonResponse {
+
+  /**
+   * The array of ajax commands.
+   *
+   * @var array
+   */
+  protected $commands = array();
+
+  /**
+   * Add an AJAX command to the response.
+   *
+   * @param $command An AJAX command object implementing AjaxCommandInterface.
+   */
+  public function addCommand($command) {
+    $this->commands[] = $command->render();
+  }
+
+  /**
+   * Sets the response's data to be the array of AJAX commands.
+   */
+  public function ajaxPrepare() {
+    parent::setData($this->commands);
+  }
+
+}
\ No newline at end of file
diff --git a/core/modules/file/file.module b/core/modules/file/file.module
index 4e6c8eb..f3d7e9d 100644
--- a/core/modules/file/file.module
+++ b/core/modules/file/file.module
@@ -797,9 +797,10 @@ function file_ajax_upload() {
   if (empty($_POST['form_build_id']) || $form_build_id != $_POST['form_build_id']) {
     // Invalid request.
     drupal_set_message(t('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (@size) that this server supports.', array('@size' => format_size(file_upload_max_size()))), 'error');
-    $commands = array();
-    $commands[] = ajax_command_replace(NULL, theme('status_messages'));
-    return array('#type' => 'ajax', '#commands' => $commands);
+    $response = new AjaxResponse();
+    $response->addCommand(new AjaxCommandReplace(NULL, theme('status_messages')));
+    $response->ajaxPrepare();
+    return $response;
   }
 
   list($form, $form_state) = ajax_get_form();
@@ -807,9 +808,10 @@ function file_ajax_upload() {
   if (!$form) {
     // Invalid form_build_id.
     drupal_set_message(t('An unrecoverable error occurred. Use of this form has expired. Try reloading the page and submitting again.'), 'error');
-    $commands = array();
-    $commands[] = ajax_command_replace(NULL, theme('status_messages'));
-    return array('#type' => 'ajax', '#commands' => $commands);
+    $response = new AjaxResponse();
+    $response->addCommand(new AjaxCommandReplace(NULL, theme('status_messages')));
+    $response->ajaxPrepare();
+    return $response;
   }
 
   // Get the current element and count the number of files.
@@ -835,16 +837,17 @@ function file_ajax_upload() {
   else {
     $form['#suffix'] .= '<span class="ajax-new-content"></span>';
   }
-
   $output = theme('status_messages') . drupal_render($form);
   $js = drupal_add_js();
   $settings = call_user_func_array('array_merge_recursive', $js['settings']['data']);
 
-  $commands = array();
-  $commands[] = ajax_command_replace(NULL, $output, $settings);
-  return array('#type' => 'ajax', '#commands' => $commands);
+    $response = new AjaxResponse();
+    $response->addCommand(new AjaxCommandReplace(NULL, $output, $settings));
+    $response->ajaxPrepare();
+    return $response;
 }
 
+
 /**
  * Ajax callback: Retrieves upload progress.
  *
