十月Cms - 如何使用前端窗体保存png图像?

时间:2017-12-29 12:02:42

标签: javascript php html octobercms

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

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

1 个答案:

答案 0 :(得分:1)

首先,您需要使用ajax Api支持文件上传的10月cms的最新版本。

{{ 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() }}
  

文件:'true'是必需的。

现在在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'
];

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