Jump to:

22997 Posts in 11853 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » UploadField for Image and File

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 501 Views
  • DairyPrincess
    Avatar
    Community Member
    8 Posts

    UploadField for Image and File Link to this post

    Hi, I'm new to SilverStripe and I want to upload Files such as doc, docx, pdf, txt as well as jpg, png...
    what is a good way to do this?

    I currently have this,

    <?php
    class ResourcesPage extends Page {
       static $db = array(
    );
       
       static $has_one = array(
          'UploadedFile' => 'File',
          'UploadedImage' => 'Image'
       );
       
       public function populateDefaults(){
          parent::populateDefaults();
       }
       
       public function getCMSFields() {
    $fields = parent::getCMSFields();
          
          $fileUpload = new UploadField( 'UploadedFile', 'Select PDF file' );
          $imageUpload = new UploadField( 'UploadedImage', 'Select image' );
          
          $fields->addFieldToTab( 'Root.Files', $fileUpload );
          $fields->addFieldToTab( 'Root.Files', $imageUpload );

    return $fields;
    }
    }
    class ResourcesPage_Controller extends Page_Controller {
    }


    Is it possible to have one UploadField that can upload a File/Image?

    in my template

          <a href="$UploadedFile.Link" >Download Link</a>
          <a href="$UploadedImage.Link" >Download Link</a>
          


    In the front end, a link will be provided so the user can download the File/Image

    not sure about the

       static $has_one = array(
          'UploadedFile' => 'File',
          'UploadedImage' => 'Image'
       );

    any suggesstions?

  • Liam
    Avatar
    Community Member
    470 Posts

    Re: UploadField for Image and File Link to this post

    The new UploadField does support multiple files.

    You currently have it setup using a $has_one relationship which will only allow it one file. Change it to a $many_many and you will be able to upload as many files as you want, both images and files. No need to setup 2 different relationships for image and file. Then just loop through your template to access the files.

  • DairyPrincess
    Avatar
    Community Member
    8 Posts

    Re: UploadField for Image and File Link to this post

    Thanks,
    yeah my teammate told me the same thing
    got an error before,
    guess it wasn't because of that.
    my bad

    501 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.