Wednesday, May 9, 2012

Re: CakePHP (2.1) Media Plugin - Multi File Upload

Well this whole project has made me realise how much I suck at Ajax/JS
etc. I ended up getting this working using cakephp's HTML5 multi file
support and converting that data array to one that the media plugin
would understand (as per Jeremy's first suggestion!).

In case anybody else is looking to do this...

In the view add to your form:
echo $this->Form->input('files.', array('type' => 'file',
'multiple'));

So that'll allow you to select multiple files.

In the controller do something like:
$i = 0;
foreach ($this->request->data['Model']['files'] as $file) {
$this->set($this->request->data['Photo'][$i]['model'] = 'Model');
$this->set($this->request->data['Photo'][$i]['group'] = 'group');
$this->set($this->request->data['Photo'][$i]['file'] = $this->request-
>data['Model']['files'][$i]);
$i++;
}

Then make sure you have saveAll:
if ($this->Model->saveAll($this->request->data)) { ...

I'm going to spend some time sorting it out better and making it as
reusable as possible. This solution is good for me as it's for an
intranet site so I have control over what browser people are using.
And if the users's browser doesn't support html5 then they can still
upload, just one file at a time.

Thanks for your suggestions Jeremy.


On May 1, 11:27 pm, jeremyharris <funeralm...@gmail.com> wrote:
> I don't have any templates, but I use file-uploader[1] on a currently
> active project. It doesn't require jQuery but works pretty nicely.
>
> It sends each upload as a separate request, so write the controller code as
> if you're receiving one file (like the media plugin examples) and you
> should be good. This method is preferred, because when javascript is
> disabled the logic in the controller doesn't need to change will still work
> (i.e., old school single upload).
>
> 1:https://github.com/valums/file-uploader (doesn't seem to be maintained
> by the author but there are numerous forks)
>
>
>
>
>
>
>
> On Monday, April 30, 2012 8:09:33 PM UTC-7, double07 wrote:
> > Hi Jeremy,
>
> > Not having much luck with the saving manually option, do you having
> > any working examples of drag and drop (jquery) I can look at?
>
> > Thanks,
>
> > -Brett
>
> > On Apr 17, 7:44 am, jeremyharris <funeralm...@gmail.com> wrote:
>
> > > Or, instead of using the multiple option, you can try processing them
> > each
> > > as a single request so you don't have to change anything in your
> > controller
> > > but rather just how the view presents it. I've used drag and drop jQuery
> > > plugins and the like to help with this. It's by far my favorite
> > solution,
> > > because it doesn't rely on browser specific features (such as HTML5
> > > multiple upload).

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate