Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

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

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

UploadField for Image and File


Go to End
Reply


3 Posts   910 Views

Avatar
DairyPrincess

Community Member, 8 Posts

13 December 2012 at 9:49pm

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? :)

Avatar
Liam

Community Member, 470 Posts

15 December 2012 at 11:20am

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.

Avatar
DairyPrincess

Community Member, 8 Posts

4 January 2013 at 4:29am

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