Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSergei Makarov
13,839 PointsRepresenting bytes[] into the input element by editing GIF-file ?
Hi there,
Is there any possibility to represent bytes[]/name of the already uploaded file in the editing form. Maybe I just want to edit Category or Description for this very file. How it could be made?
By adding th:field ="*{bytes}" into the input form field doesn't make a lot of sense.
Any suggestions?
3 Answers
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsWhy do you need to represent bytes
field in thymeleaf? You don't need to put all of the member variables of the object for the form to get through.
If you want to edit Gif, you get all "good" changed data, like 'name', 'description'. And then before you save Gif, you take the old image of it, and put it to the Gif you are trying to update?
Does it make sense? Sometimes you can have situations where you want to change only specific property of object, like name, and there is no need to put all the rest of the data to thymeleaf. In this case you put to thymeleaf template only id and name, leaving the rest of the fields to be null.
Then All you do is take old Object from database, change his name to the new one, and update it. Without carrying around information of all his fields, but just taking the data from database.
That is if I understood your question correctly...
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsLet me show workaround how to change all the fields except image:
- Add
update(Gif gif)
method on Service Layer, like this:
@Override
public void update(Gif gif) {
Gif oldGif = gifDao.findById(gif.getId());
gif.setBytes(oldGif.getBytes());
gifDao.save(gif);
}
- Add the following to the
GifController.updateGif
method:
@RequestMapping(value = "/gifs/{gifId}", method = RequestMethod.POST)
public String updateGif(Gif gif,
@RequestParam MultipartFile file,
RedirectAttributes redirectAttributes) {
// TODO: Update GIF if data is valid
if (!file.isEmpty()) {
gifService.save(gif, file);
} else {
System.out.println("file is empty");
gifService.update(gif);
}
This way we can use old image if file is empty and update Gif, and put new one if new image was uploaded.
Coming back to the problem how to show existing image in input
I don't know. May be Chris knows, may be
Google... Have to dig more
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsHi. I found this article, take a look:
http://stackoverflow.com/questions/1696877/how-to-set-a-value-to-a-file-input-in-html
I think we cannot set value
of file
to input
theoretically not just in thymeleaf, due to security reasons. So that
no one can change the value
, otherwise they can steal from you, I guess.
Which means we cannot pre-populate file input with value.
I like answer with 25 votes:
Not an answer to your question (which others have answered), but if you want to have some edit functionality of an uploaded file field, what you probably want to do is:
- show the current value of this field by just printing the filename or URL, a clickable link to download it, or if it's an image: just show it, possibly as thumbnail
- the <input> tag to upload a new file
- a checkbox that, when checked, deletes the currently uploaded file. note that there's no way to upload an 'empty' file, so you need something like this to clear out the field's value
Sergei Makarov
13,839 PointsSergei Makarov
13,839 PointsThis is exactly the problem!
Try to edit you Gif without changing anything but, for example, description, and you will see, that your Gif will be no more shown/persisted. That's the point: it will be set to NULL and NULL will be saved in DB (excepting you have a validation @NotNull on your Gif-Entity).
Either you have to write a SQL-logic in your GifService for update method or you have to check all fields again! That's the point. I want the file would be persisted (loaded) in my <input type="file"> field as all other are.
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsAlexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsI tried. You are right. Now I understand what are you trying to achieve. I will try to google around a bit to see what is possible to do.