Commit 9a3fd256 authored by pdrake's avatar pdrake
Browse files

Fixed file upload handling.

parent a63f8a1c
......@@ -28,20 +28,25 @@ class ContentNegotiation {
*/
public function getContentType(Request $request) {
$acceptable_content_types = $request->getAcceptableContentTypes();
if ($request->isXmlHttpRequest()) {
if ($request->get('ajax_iframe_upload', FALSE)) {
return 'iframeupload';
}
else {
return 'ajax';
}
// AJAX iframe uploads need special handling, because they contain a json
// response wrapped in <textarea>.
if ($request->get('ajax_iframe_upload', FALSE)) {
return 'iframeupload';
}
// AJAX calls need to be run through ajax rendering functions
elseif ($request->isXmlHttpRequest()) {
return 'ajax';
}
if (in_array('application/json', $acceptable_content_types)) {
// JSON requests can be responded to using JsonResponse().
elseif (in_array('application/json', $acceptable_content_types)) {
return 'json';
}
// Do HTML last so that it always wins for */* formats.
if(in_array('text/html', $acceptable_content_types) || in_array('*/*', $acceptable_content_types)) {
elseif(in_array('text/html', $acceptable_content_types) || in_array('*/*', $acceptable_content_types)) {
return 'html';
}
}
......
......@@ -31,28 +31,6 @@ public function __construct(ContentNegotiation $negotiation) {
$this->negotiation = $negotiation;
}
protected function createAjaxResponse(GetResponseEvent $event) {
$response = new Response();
$response->headers->set('Content-Type', 'application/json; charset=utf-8');
return $response;
}
protected function createIframeUploadResponse(GetResponseEvent $event) {
$response = new Response();
// Browsers do not allow JavaScript to read the contents of a user's local
// files. To work around that, the jQuery Form plugin submits forms containing
// a file input element to an IFRAME, instead of using XHR. Browsers do not
// normally expect JSON strings as content within an IFRAME, so the response
// must be customized accordingly.
// @see http://malsup.com/jquery/form/#file-upload
// @see Drupal.ajax.prototype.beforeSend()
$response->headers->set('Content-Type', 'text/html; charset=utf-8');
return $response;
}
/**
* Processes a successful controller into an HTTP 200 response.
*
......@@ -98,7 +76,7 @@ public function onAjax(GetResponseEvent $event) {
$json = ajax_render($commands);
// Build the actual response object.
$response = $this->createAjaxResponse($event);
$response = new JsonResponse();
$response->setContent($json);
return $response;
......@@ -117,13 +95,9 @@ public function onIframeUpload(GetResponseEvent $event) {
// JSON data by making it the value of a textarea.
// @see http://malsup.com/jquery/form/#file-upload
// @see http://drupal.org/node/1009382
$json = '<textarea>' . $json . '</textarea>';
$html = '<textarea>' . $json . '</textarea>';
// Build the actual response object.
$response = $this->createIframeUploadResponse($event);
$response->setContent($json);
return $response;
return new Response($html);
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment