TYPO3 Flow Resource Management
The documentation [1] on flow.typo3.org about the resource management is already finished. There are some nice examples and the resource management is working good in Flow 1.x
Anyway, when I played around with it some time ago I missed one point, so I try to explain it in a small example. I wanted to upload some csv files and I wanted to work with the data. So this example shows how to upload a file and use the object later on.
Create the upload form
Nothing new here I guess. We just use the Fluid form view helper for creating our form and we set the type of our field to f:form.upload and we are done. Now you can see the input button on your page to select a file you want to upload.
1 2 3 4 5 |
<f:form action="uploadFile" controller="FileUpload" enctype="multipart/form-data"> <f:form.upload name="fileUpload" /> <br /> <f:form.submit value="upload file"/> </f:form> |
Create the fileUpload action
First part is almost the same like in the example on flow.typo3.org. What took me some time is to find out how I can access the file to work with the data in the file.
It is really easy and I can’t remember what took me so long. What you have to do is to get the uri of the resource object. As soon as you have the resource uri, you can use it like a file name and do, like in my example, an fopen on the file and get the content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/** * uploadFile action * * @param array $fileUpload */ public function uploadFileAction(array $fileUpload) { /* @var $fileResource \TYPO3\Flow\Resource\Resource */ $fileResource = $this->resourceManager->importUploadedResource($fileUpload); $uri = $fileResource->getUri(); // read file $csvLines = array(); $fHandle = \fopen($uri, 'r'); if ($fHandle) { while (($buffer = \fgets($fHandle, 4096)) !== false) { $csvLines[] = $buffer; } } \fclose($fHandle); } |
Create a download link
1 |
<a src="{f:uri.resource(resource: MyResource.resource)}">Download</a> |
Hi
thanks for the hints.
Why is array $fileUpload?
I try something veru similar, and get “Required argument “fileUpload” is not set.”. Any further hint?
best m.
Hi Mario,
sorry for the late response, I was on vacation. The array $fileUpload is the name of the form field where you can add the files. It will be assigned to the action automatically. So whatever you set as argument, you have to name the formElement with the same name.
Hope this helps.
Greetings,
Thomas
Thanks for that 🙂