douliang2935 2016-05-16 06:59
浏览 92

Octobercms上传文件

How to solve the error?

image

this is my htm code (button upload)

{{ form_open({files: true, request: 'onFileUpload'}) }}

<!--File Input-->
<input type="file" name="file-upload" required="required">
<!--File Input-->

<!--Submit/Upload Button-->
<button type="submit">Upload</button>

{{ form_close() }}

this is the component php code

     public function onFileUpload()
{
    $file = new System\Models\File;
    $file->data = Input::file('file-upload');
    $file->save();

    // Attach the uploaded file to your model
    $model->file()->add($file); 
   // The above line assumes you have proper attachOne or attachMany relationships defined on your model

    $model->file_path = url('/') . $model->file->getPath();
    $model->save();

    return Redirect::back();
}

is this the proper attachMany relationship?

public $attachMany = [
'files' => 'System\Models\File',
];
}    

I'm not very sure about the code cause I'm new to October cms Can anyone show some examples? How to create a drag and drop file uploader component?

  • 写回答

1条回答 默认 最新

  • dongyi2159 2018-09-24 08:46
    关注

    You need to new up an instance of the model that you've defined you relationship in and set it as your $model variable.

    For example.. your model class could look like this.

    class User extends Model
    {
        public attachMany [
            'files' => 'System\Models\File'
        ];
    }
    

    And then in your component onFileUpload()

    $model = new User;
    $model->files()->add($file); 
    $model->file_path = url('/') . $model->file->getPath();
    $model->save();
    

    Also, just take notice that your attachMany relationship in your model class is defined as files but you're trying to use it with

    $model->file()->add();
    

    It should be

    $model->files()->add();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化