I try to add a Category of a blogpost. A category has a name and an image. I'm going to upload this image using Dropzone.js.
Now i'm a little bit confused since the image upload happens asynchronously, so the image is uploaded before(!) all the other posts (like the name and the entire form itself) are sended.
If I do this directly via post request (just hit the submitbutton with a simple file input field), live is much easier, because i'm redirected to the page (using the redirect() in the controller) and every "magic" happens behind the scenes through laravel.
But this doesn't seem to work with an asynchronous request, since the redirect is an http redirect, so there's no redirect at all. Furthermore any flashmessages (especially in case of errors) are ignored either, because this depends on sessions.
I end up having a lot of problems to handle this. Is there a "quick solution" or is ist better to do it "the dirty way"?
Why don't you upload the images separately through another controller? You could add the image information to the database and return its ID in json and attach it to the post form using javascript.
Okay - sounds a little tricky but doable. But how would I handle this redirect issue then? I'd probably do that with location.href, but then the url is missing.
Edit: I extracted the url from the response header:
But what about the Session? The response code has got the Flash text - so I can extract it. That would work as long as it's an error, because then there's no redirect. But if everything is okay, the flash message is gone!
Dropzone has a complete and error event. The controller that deals with the images can send json back when it's successful or when it fails, so you can add some code to show an error message to the error event. I wouldn't use any redirect on that image controller and use a separate form for the image uploads. Then on success, get dropzone to add the id of the image to a hidden input on the other form.
But if you are not sure what you are doing just stick to a regular file input element. Everything will be sent together through a traditional POST. You can find a tutorial here using the <input type="file"/> method; http://devdojo.com/video/82