Download multiple images (Play Framework)

I try to allow the user to upload multiple images, but when I check the form with some files, I get this error Incorrect value in the image field ... Here is my code:

in view:

#{form @save(id)} #{ifErrors} <p class="error"> Please correct these errors. </p> #{/ifErrors} <p> <label>Titre</label> <input type="text" name="title" value="${flash.title}" id="title" /> <span class="error">#{error 'title' /} </p> <p> <label>Description</label> <textarea name="detail" value="${flash.detail}" id="detail" /></textarea> <span class="error">#{error 'detail' /} </p> <p> <label>Photos</label> <input type="file" draggable="true" name="files" id="files" multiple="multiple"/> <strong>(Max. 5 photos)</strong> <span class="error">#{error 'files' /} </p> <p> <input type="submit" value="Publier l'annonce" /> </p> #{/form} 

java function:

  public static void save(long id, @Required String title, @Required String detail, File[] files) throws IOException{ if (id == 0){ validation.keep(); params.flash(); flash.error("Vous devez préalablement selectionner une catégorie..."); Application.setclassified(); } // Action when form errors found else if (validation.hasErrors() ) { validation.keep(); params.flash(); flash.error("Veuillez corriger les erreurs..."); form(id); } //save uploaded images for (File file : files) { FileInputStream is = new FileInputStream(file); String original = "/data/" + file.getName(); IOUtils.copy(is, new FileOutputStream(Play.getFile(original))); } } 
+4
source share
2 answers

You use an HTML5 field to upload a DnD file, don’t know? I don’t know what functions JS provides, but usually when you drop a file, it automatically downloads it. I suggest you use the YouTube upload function. For instance:

After downloading the file, return json with the generated file identifier, so when saving records, you can link it to the file you downloaded.

When I played with it, as a rule, javascript will open a new POST request, this request should get it from request.body , which is InputStream , use InputStream with IOUtils to save the file after saving return a JSON object with the file name or path file.

Thing is, I don’t think it will ever go through File files[] because javascript opens a new POST request, so it will always return the wrong value since it never populates when you send data to the server.

You can try: create two routes: one that downloads a file that saves the metadata of the file, using one method that you do all the file processing, and the other takes a json response and binds it then save.

I also posted here some code examples for a similar question: PlayFramework: Ajax + Drag n 'Drop + File Download + File Object in the controller?

Greetings, I'm sorry if I feel a little redundant, as if tired, but I hope this helps you :)

+8
source

File uploads must be forced to "multipart / form-data" for files to be processed as part of playback. Otherwise, the file array will be zero.

Adjust the shape as follows:

  #{form @save(id), enctype:'multipart/form-data'} 
+3
source

Source: https://habr.com/ru/post/1340548/


All Articles