Skip to content

Commit

Permalink
FIX #3458 iframe transport multi file upload FIX #3343, FIX #3148
Browse files Browse the repository at this point in the history
UploadField now handles multiple file upload through iframe transport
correctly (mainly for IE) as well as upload errors on a per file basis.
  • Loading branch information
colymba committed Sep 26, 2014
1 parent c57c24e commit bbc1cb8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
9 changes: 9 additions & 0 deletions filesystem/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,11 @@ public function setFile($file) {

/**
* Clear out all errors (mostly set by {loadUploaded()})
* including the validator's errors
*/
public function clearErrors() {
$this->errors = array();
$this->validator->clearErrors();
}

/**
Expand Down Expand Up @@ -342,6 +344,13 @@ public function getErrors() {
return $this->errors;
}

/**
* Clear out all errors
*/
public function clearErrors() {
$this->errors = array();
}

/**
* Set information about temporary file produced by PHP.
* @param array $tmpFile
Expand Down
25 changes: 15 additions & 10 deletions forms/UploadField.php
Original file line number Diff line number Diff line change
Expand Up @@ -1222,20 +1222,25 @@ public function upload(SS_HTTPRequest $request) {
$name = $this->getName();
$postVars = $request->postVar($name);

// Save the temporary file into a File object
// Extract uploaded files from Form data
$uploadedFiles = $this->extractUploadedFileData($postVars);
$firstFile = reset($uploadedFiles);
$file = $this->saveTemporaryFile($firstFile, $error);
if(empty($file)) {
$return = array('error' => $error);
} else {
$return = $this->encodeFileAttributes($file);
$return = array();

// Save the temporary files into a File objects
// and save data/error on a per file basis
foreach ($uploadedFiles as $tempFile) {
$file = $this->saveTemporaryFile($tempFile, $error);
if(empty($file)) {
array_push($return, array('error' => $error));
} else {
array_push($return, $this->encodeFileAttributes($file));
}
$this->upload->clearErrors();
}

// Format response with json
$response = new SS_HTTPResponse(Convert::raw2json(array($return)));
$response = new SS_HTTPResponse(Convert::raw2json($return));
$response->addHeader('Content-Type', 'text/plain');
if (!empty($return['error'])) $response->setStatusCode(403);
return $response;
}

Expand Down
9 changes: 5 additions & 4 deletions tests/forms/uploadfield/UploadFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,18 @@ public function testAllowedExtensions() {
'UploadFieldTest_Controller/Form/field/AllowedExtensionsField/upload',
array('AllowedExtensionsField' => $this->getUploadFile($invalidFile))
);
$this->assertTrue($response->isError());
$this->assertContains('Extension is not allowed', $response->getBody());
$response = json_decode($response->getBody(), true);
$this->assertTrue(array_key_exists('error', $response[0]));
$this->assertContains('Extension is not allowed', $response[0]['error']);

$validFile = 'valid.txt';
$_FILES = array('AllowedExtensionsField' => $this->getUploadFile($validFile));
$response = $this->post(
'UploadFieldTest_Controller/Form/field/AllowedExtensionsField/upload',
array('AllowedExtensionsField' => $this->getUploadFile($validFile))
);
$this->assertFalse($response->isError());
$this->assertNotContains('Extension is not allowed', $response->getBody());
$response = json_decode($response->getBody(), true);
$this->assertFalse(array_key_exists('error', $response[0]));
}

/**
Expand Down

0 comments on commit bbc1cb8

Please sign in to comment.