October Cms - 如何使用前端表单保存 png 图像?

October Cms - How can I save a png image using a frontend form?

我希望用户能够上传图片以在页面中显示它们。

我尝试了不同的方法,但无法上传。

首先您需要最新版本的 October cms,因为它支持使用 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.

现在在您的 component 或您的 page code section 中,您可以编写代码

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

    //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');
    $yourModel->save();
}

在您的模型中可以添加关系

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

它将允许您使用 AJAX 上传文件,如果您需要 POST 上传文件的方法,请评论。