如何在TYPO3前端扩展中实现多文件上传

时间:2015-06-30 10:38:05

标签: typo3 extbase

我的要求是在TYPO3前端扩展中实现多个文件上载字段。以下是我用于单个文件上传的内容。

模型中的我的字段

/**
 * angebotuploaden
 *
 * @var \TYPO3\CMS\Extbase\Domain\Model\FileReference
 */
protected $angebotuploaden = NULL;

 /**
 * Returns the angebotuploaden
 *
 * @return \TYPO3\CMS\Extbase\Domain\Model\FileReference $angebotuploaden
 */
 public function getAngebotuploaden() {
 return $this->angebotuploaden;
}

/**
 * Sets the angebotuploaden
 *
 * @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $angebotuploaden
 * @return void
 */
public function setAngebotuploaden(\TYPO3\CMS\Extbase\Domain\Model\FileReference $angebotuploaden) {
    $this->angebotuploaden = $angebotuploaden;
}

现在我在为此字段实施多个文件上传时遇到问题。请帮我解决一下。

1 个答案:

答案 0 :(得分:3)

使用ObjectStorage获取FileReference模型的1:n-Relation。在您的模型中可能如下所示:

/**
 * uploadFiles
 *
 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
 * @cascade remove
 */
protected $uploadFiles = NULL;

/**
 * __construct
 */
public function __construct() {
    //Do not remove the next line: It would break the functionality
    $this->initStorageObjects();
}

/**
 * Initializes all ObjectStorage properties
 * Do not modify this method!
 * It will be rewritten on each save in the extension builder
 * You may modify the constructor of this class instead
 *
 * @return void
 */
protected function initStorageObjects() {
    $this->uploadFiles = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
}



/**
 * Adds a UploadFile
 *
 * @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $uploadFile
 * @return void
 */
public function addUploadFile(\TYPO3\CMS\Extbase\Domain\Model\FileReference $uploadFile) {
    $this->uploadFiles->attach($uploadFile);
}

/**
 * Removes a UploadFile
 *
 * @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $uploadFileToRemove The UploadFile to be removed
 * @return void
 */
public function removeUploadFile(\TYPO3\CMS\Extbase\Domain\Model\FileReference $uploadFileToRemove) {
    $this->uploadFiles->detach($uploadFileToRemove);
}

/**
 * Returns the uploadFiles
 *
 * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> $uploadFiles
 */
public function getUploadFiles() {
    return $this->uploadFiles;
}

/**
 * Sets the uploadFiles
 *
 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> $uploadFiles
 * @return void
 */
public function setUploadFiles(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $uploadFiles) {
    $this->uploadFiles = $uploadFiles;
}   

还有更多事情要做,特别是在TCA,但我不详细了解它们,因为我还没有使用它。有关更多详细信息,请参阅Hemult Hummels Upload Examplethis question