Using Laravel 5.2
, I am working on an upload form which should allow the user to select up to 10 images, display their previews and then let him sort them arround, add more images or remove the ones he doesn't want to upload.
The sorting and removal of previews can quite easily be done using javascript but beyond that I am having a hard time figuring out how to properly do the rest. My original idea was to get the js file array from the input, move it to another array and work with that once the user interacts with the image previews, which would be displayed using FileReader
.
Once he submits the form I would create a FormData
object, remove the current files in input and add all the valid ones:
var formdata = new FormData(document.getElementById('upload-form'));
if (formdata.has('pic[]') && validFiles.length) {
formdata.delete("pic[]");
var len = validFiles.length;
for (i = 0; i < len; i++) {
formdata.append("pic[]", validFiles[i]);
}
}
If you know your way around js you can already see that this approach has severe problems when it comes to backwards compatibillity, as the FormData.delete(..)
and .has(..)
methods are not supported by older browsers (including Chrome 50 and below or pretty much any IE.)
Ok scrap that idea. My second idea was to do the following:
- User selects images to add
- Upload said images using ajax and store them temporarily somewhere using Laravel
- Return image previews
- Let the user use the previews to reorder the files or remove them and keep track of what he does.
- Once the form is submitted take all the other info, figure out which files are to be stored forever and which ones to delete and save.
Now I am not quite sure how to do this. I was thinking that I coud have a tmp directory for these images, generate some sort of a string to be the name of the directory associated with the users session, store the files there and save the directory name using Session
.
The problem with this is that Session
seems to have a global timeout and cannot be set to expire in say 15 minutes. Another problem I see is that once the Session
expires and the user decides to not upload the images they would stay on the server forever as I couldn't find a method that would allow me to execute code upon Session
expiry.
So what is the proper way to do temporary file storage in Laravel? Or am going in the wrong direction here and can you see a better way to do what I am attempting to do?
Thanks in advance for any advice.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire