简体   繁体   中英

Frontend image upload and build a file reference in typo3

First, im really new at typo3. I build a extension with extension builder, and now i want to upload images from the Frontend. The upload and the creating Folder function is working fine. But typo3 doesnt output the file after upload. According to several google searches i think it has to do with the file_reference function. But im really stuck on this. Hope somebody can heelp me on this.

Heres my creatAction Funnction:

 public function createAction(\Demian\Inserateextension\Domain\Model\myObject $newObject)
    {


    $chiffre = $_POST['tx_myplugin']['myObject']['chiffre'];
    $newImagePath = 'inserate/'.$chiffre;


    if ($_FILES['tx_myplugin']['name']['gallerie'][0]) {


        $tmpName = $_FILES['tx_myplugin']['name']['gallerie'][0];
        $tmpFile  = $_FILES['tx_myplugin']['tmp_name']['gallerie'][0];

        $storageRepository = $this->objectManager->get('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
        $storage = $storageRepository->findByUid('1'); //this is the fileadmin storage

        //build the new storage folder
        $targetFolder = $storage->createFolder($newImagePath);

     //Upload the file
        $movedNewFile = $storage->addFile($tmpFile, $targetFolder, $tmpName);



}

    $this->myObjectRepository->add($newObject);
    $this->redirect('list');
}

Maybe you should have a look in this repository. It is a good example how to handle file uploads with FAL in TYPO3. Maybe a little old, but the mechanics should become clear. https://github.com/helhum/upload_example

This is what i have so far:

public function createAction(\Demian\Inserateextension\Domain\Model\myObject $newObject)
    {


    $chiffre = $_POST['tx_myplugin']['myObject']['chiffre'];
    $newImagePath = 'inserate/'.$chiffre;


    if ($_FILES['tx_myplugin']['name']['gallerie'][0]) {


        $tmpName = $_FILES['tx_myplugin']['name']['gallerie'][0];
        $tmpFile  = $_FILES['tx_myplugin']['tmp_name']['gallerie'][0];

        $storageRepository = $this->objectManager->get('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
        $storage = $storageRepository->findByUid('1'); //this is the fileadmin storage

        //build the new storage folder
        $targetFolder = $storage->createFolder($newImagePath);

     //Upload the file
        $movedNewFile = $storage->addFile($tmpFile, $targetFolder, $tmpName);



}

    $this->myObjectRepository->add($newObject);
    $this->redirect('list');
}



$this->buildRelations($newObject->getUid(), $movedNewFile, 'gallerie', '    tx_myplugin', $storagePid);

private function buildRelations($newStorageUid, $file, $field, $table, $storagePid) {

        $data = array();
        $data['sys_file_reference']['NEW1234'] = array(
            'uid_local' => $file->getUid(),
            'uid_foreign' => $newStorageUid, 
            'tablenames' => $table, 
            'fieldname' => $field, 
            'pid' => $storagePid,
            'table_local' => 'sys_file',
        );
        $data[$table][$newStorageUid] = array('image' => 'NEW1234'); 
        /** @var \TYPO3\CMS\Core\DataHandling\DataHandler $tce */
        $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\DataHandling\DataHandler'); // create TCE instance
        $tce->start($data, array());
        $tce->process_datamap();
    }

So now it uploads the file and a DB entry in sys_file, and in my_exteension_domain_model_myObject is created but the image path is still not showin in the fluid var "myObject.gallerie.originalResource.publicUrl". Also the column "l10n_diffsource" is null, but shoud be something like:

a:7{s:16:"sys_language_uid";N;s:6:"hidden";N;s:5:"title";N;s:7:"chiffre";N;s:8:"gallerie";N;s:9:"st…

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM