This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

jfo
jfo

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

Crazymodder
Crazymodder

Hi,

Simple call:

$model->importFile->getPath();

Regards Crazymodder

jfo
jfo

Hi @Crazymodder Thanks for replying!

I've tried that, but it throws the exception Call to a member function getPath() on null.

I also tried

$model->importFile()->getPath();

Which raises the error Call to undefined method October\Rain\Database\QueryBuilder::getPath().

Is there any additional steps to be performed before being able to invoke the getPath method?

Crazymodder
Crazymodder

Hi jfo,

does your model object really called $model ? That was only an example. You have to replace it with your instance name than it should not be null ;)

jfo
jfo

Hi @Crazymodder,

The $model is not null the $model->importFile is null. $model was just to keep my example simple. My variable is named $myModel as you can see in the code sample on my original post.

Crazymodder
Crazymodder

Hi,

Where do you call $model->importFile ?

jfo
jfo

I tried on several places: 1) In the model beforeCreate and beforeSave events;

2) Also on a controller AJAX method handler - in this case the onImportFile method;

The behavior is pretty consistent, no matter where I'm calling the importFile..

Here is what I have working now, although not as clean or straightforward as I would expect:

    public function beforeCreate()
    {
        $sessionKey = \Input::get('_session_key');

        // returns the latest file uploaded in the current session
        // is this a reliable approach?
        $file = $this
            ->import_file()
            ->withDeferred($sessionKey)
            ->latest()
            ->first();
        dd($file->getLocalPath());    
        return false;
    }

Could you post any piece of working code accessing an uploaded file using a different approach?

Last updated

pwtan14262
pwtan14262

Hi jfo,

Any update on the issue?

philipptempel
philipptempel

I might be wrong on this one but this is how I understand October: If you have a file attached to a model (be it attachOne or attachMany) and you upload a file to the not yet created model, it will be saved as a deferred model. Thus, you cannot access $this->import_file() in the beforeCreate() method as there is no file assigned yet. This will be done after the model is created. As such you need to first create your model and then assign the file to it. On your second question (how to retrieve the deferred binding session key): no idea, but maybe you want to have a look at Form::sessionKey() to see how it is being created. Reading it should then be very simple (I am assuming it is just a $_SESSION key-value.

jfo
jfo

@pwtan14262: the code I posted above it's working. I don't think it's an elegant solution, but that's the best I could come up with.

@philipptempel: the session key issue is solved in my previous comment using

$sessionKey = \Input::get('_session_key');

Last updated

pwtan14262
pwtan14262

Hi jfo,

Thanks a lot. It may not be elegant but at least it does the job. Great work.

info16697
info16697

jfo said:

I tried on several places: 1) In the model beforeCreate and beforeSave events;

2) Also on a controller AJAX method handler - in this case the onImportFile method;

The behavior is pretty consistent, no matter where I'm calling the importFile..

Here is what I have working now, although not as clean or straightforward as I would expect:

   public function beforeCreate()
   {
       $sessionKey = \Input::get('_session_key');

       // returns the latest file uploaded in the current session
       // is this a reliable approach?
       $file = $this
           ->import_file()
           ->withDeferred($sessionKey)
           ->latest()
           ->first();
       dd($file->getLocalPath());    
       return false;
   }

Could you post any piece of working code accessing an uploaded file using a different approach?

Hi, having issues with uploading images on the backend, retrieving their path to insert in the database with my model. Main problem is getting the path to the uploaded image. Where exactly did you put the code above? In the Controller? or the Model?

code200.miha
code200.miha

Can someone help me how to get path of the file in my random class if file is uploaded in backend form. I can access all other settings without problem but no luck with getting any info about the file.

code200.miha
code200.miha

Can someone help me how to get path of the file in my random class if file is uploaded in backend form. I can access all other settings without problem but no luck with getting any info about the file.

code200.miha
code200.miha

I guess I figured it out.

In class you need to access Settings model as instance and then you can just call uploaded file variable to get back File instance.

$settings = Settings::instance();
//@var System\Models\File $uploadedFile
$uploadedFile = $settings->uploaded_file

1-15 of 15

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.