Hi,
I'm struggling for hours trying to access a file uploaded using the fileupload widget in my form.
I'm not uploading when saving the model, but actually when clicking a submit button calling a method handler.
Here is the closest I've got, reading the file from the method handler on my controller:
fields.yaml:
importFile:
label: 'File to import'
type: fileupload
mode: file
fileTypes: txt
required: true
useCaption: false
update.htm:
<button
type="submit"
class="btn btn-danger"
data-request="onImportFile"
data-request-confirm="Are you sure you want to import the selected file?"
data-popup-load-indicator>
Import file
</button>
Model:
public $attachOne = [
'importFile' => ['System\Models\File']
];
Controller:
public function onImportFile($recordId = null)
{
$myModel = $this->formFindModelObject($recordId);
$file = $myModel
->importFile()
->withDeferred('K2QeJwqVPDgEdjURvuYh1qNcuEMSjdNxXiqJmNXN') // how to get this session key dynamically?
->first();
var_dump(" file path", $file->getLocalPath()); // it works!
}
Questions:
1) Is this the right approach to access the file path of an uploaded file?
2) How to dynamically get the sessionKey to use as a parameter when calling the withDeferred method?
Important to notice that I don't actually need to store the file. I just need to upload the file, read it and do something with its content.
Thanks!
Last updated