srs81 / CakePHP-AjaxMultiUpload

CakePHP 2 plugin to allow for easy multi-file upload with AJAX
Other
65 stars 33 forks source link

Adding a file from add() #9

Open bobartlett opened 12 years ago

bobartlett commented 12 years ago

I really like how this uploader works! One thing that I am trying is to add files when I am initially adding a record. I can add files during the edit process, but it seems like you can't add files until after a record has been saved and there is an id. Is there a way around this?

srs81 commented 12 years ago

Excellent question! Unfortunately I am not sure how to make this work. :/ The uploader relies on the model name and ID to correctly organize the directory structure, since it has no other way to upload the files without having some ID or unique identifier as a relationship.

One way I can think of doing this is to upload to a temporary directory, and then move the files to the ID once the model is created. However: I do not know how this could be effectively using CakePHP plumbing.

bobartlett commented 12 years ago

Good idea about the temp directory. afterSave() could be used to move the files after the record is committed and an id is available. I will let you know if I get this working.

srs81 commented 12 years ago

Nice! Using afterSave() is a great idea, though you would still need to have an idea of which temp directory holds the right files and pass that along as well ... if you do get it working and would like to contribute the code back upstream, please create a pull request and I'll merge it in!

bobartlett commented 12 years ago

Will do!

stevecordle commented 12 years ago
public function add(){
    $last_entry = $this->Model->find('first', array('order' => 'created DESC'));

    $this->set('lastID', $last_entry['Model']['id']);
}

That is assuming you've added created/modified to your fields in your database. I add those fields to ever table I create, you never know how you could use them later on.

gabeidx commented 11 years ago

Is there something about this on the way? It would be very nice see this incorporated to the project.

@stevecordle I'm not sure I understand what you're proposing, but one problem I can see with your idea is the following: What if the user opens 2 browser tabs with the add action? When the first add request is sent, it will work ok, but when the second add request is sent, the last inserted ID would not be the same, thus uploading the files to the wrong folder inside webroot/files.

@srs81 @bobartlett afterSave is a Model callback and the plugin magic happens on the UploadsController file. I don't see how this could be done with the current structure of the plugin.

I'll see what I can do about this when I get some free time later this month.

srs81 commented 11 years ago

@gabrielizaias Thanks! I personally will not able to work on this soon, but if you incorporate this and send me a pull request, I can merge it.