October Cms - How Can I Save A Png Image Using A Frontend Form?

- 1 answer

I want the user to be able to upload images to show them in pages.

I tried in different ways but I couldn't get the upload working.



First of you need latest build of October cms as it supports file upload using ajax Api.

{{ form_ajax('onUploadImage', { files: 'true',  flash: 'true', 'data-request-files':true, 'data-request-validate': true }) }}
    <input type="file" name="avatar" id="avatar" />
    <button type="submit" data-attach-loading>Upload</button>
{{ form_close() }}

files: 'true' is required.

now on your component or in you page code section you can write code

public function onUploadImage() {
    // Returns the signed in user
    $user = Auth::getUser();
    $user->avatar = \Input::file('avatar');

    //this \Input::file('avatar'); do have file instance
    so with your model you can also do same 
    // $yourModel get your model instance
    $yourModel->fileRelation = \Input::file('file_input_name');

Inside your Model can add relation

public $attachOne = [
    'fileRelation' => 'System\Models\File'

it will allow you to upload file using AJAX if you need POST method to upload file please comment.