Reputation: 45
i am creating typo3 extension development.In that we have file upload i am confused regarding the directory of upload and code to be written in the controller.i know the form code of fluid i.e. but controller code i have no idea.so seeking help
Upvotes: 0
Views: 2602
Reputation: 705
The example from Helmut Hummel works for Typo3 6.2 but not for 8.x and higher. Cause of removed methods calls like getValue(false) in his own UploadViewHelper.
A other way, without a own FileReference, is to upload a file with using the FileAbstractionLayer is the following.
Frontend: Create a form for your model class. Note: The form need the attributes method="post" enctype="multipart/form-data"
<f:form action="addDownloadItem" method="post" enctype="multipart/form-data" object="{newDownloadItem}" name="newDownloadItem">
<input type="file" property="file" name="datei">
</f:form>
In your controller action write the following code:
public function addDownloadItemAction(DownloadItem $newDownloadItem, SubCategory $subCategory){
$file = $_FILES['datei'];
$storage = $this->storageRepository->findAll()[0];
$fileObject = $storage->addFile($file['tmp_name'], $storage->getDefaultFolder(), $file['name']);
$fileObject = $storage->getFile($fileObject->getIdentifier());
$this->downloadItemRepository->add($newDownloadItem);
$fileResourceReference = new \TYPO3\CMS\Core\Resource\FileReference(array('uid_local' => $fileObject->getUid()));
/** @var \TYPO3\CMS\Extbase\Domain\Model\FileReference $fileSysReference */
$fileSysReference = $this->objectManager->get(\TYPO3\CMS\Extbase\Domain\Model\FileReference::class);
$fileSysReference->setOriginalResource($fileResourceReference);
$fileSysReference->setPid($this->storagePid);
$newDownloadItem->setFile($fileSysReference);
$subCategory->addDownloadItem($newDownloadItem);
$this->subCategoryRepository->add($subCategory);
}
Now let us have a look at the model class:
class DownloadItem extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {
/**
* file
*
* @var \TYPO3\CMS\Extbase\Domain\Model\FileReference
*/
protected $file = NULL;
...
The TCA should look like the following:
'file' => array(
'exclude' => 1,
'label' => 'LLL:EXT:<<extKey>>/Resources/Private/Language/locallang_db.xlf:tx_<<extKey>>_domain_model_downloaditem.file',
'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig(
'file',
array('maxitems' => 1,
'appearance' => array(
'createNewRelationLinkTitle' => 'LLL:EXT:cms/locallang_ttc.xlf:media.addFileReference'
),
'foreign_types' => array(
'0' => array(
'showitem' => '
--palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette,
--palette--;;filePalette'
),
\TYPO3\CMS\Core\Resource\File::FILETYPE_TEXT => array(
'showitem' => '
--palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette,
--palette--;;filePalette'
),
\TYPO3\CMS\Core\Resource\File::FILETYPE_IMAGE => array(
'showitem' => '
--palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette,
--palette--;;filePalette'
),
\TYPO3\CMS\Core\Resource\File::FILETYPE_AUDIO => array(
'showitem' => '
--palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette,
--palette--;;filePalette'
),
\TYPO3\CMS\Core\Resource\File::FILETYPE_VIDEO => array(
'showitem' => '
--palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette,
--palette--;;filePalette'
),
\TYPO3\CMS\Core\Resource\File::FILETYPE_APPLICATION => array(
'showitem' => '
--palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette,
--palette--;;filePalette'
)
),
'foreign_match_fields' => array(
'tablenames' => 'tx_<<extKey>>_domain_model_downloaditem',
),
)
),
),
Note: you can change the file types foreign_types, but the important part is:
'foreign_match_fields' => array(
'tablenames' => 'tx_<<extKey>>_domain_model_downloaditem',
),
The tablenames have to be the name of your model table.
Notes:
Upvotes: -3
Reputation: 4558
If you want to do it the proper way, you should use FAL for file uploads instead of handling it yourself completely. There is an elaborate post about FAL upload with Extbase by Helmut Hummel, to be found here.
Helmut also provided a demo extension as a proof of concept, to be found on Github.
Upvotes: 4