K
K
KaizerSX2021-04-06 18:11:28
Java
KaizerSX, 2021-04-06 18:11:28

How to save an image in the table when changing other fields?

There is a table where the data from the form is pulled in which there are several text input fields and one input field with the "file" type (the picture is loaded there).

The problem is that if you change something other than the picture, the picture will not be saved after updating the data.

Here is the table (it's on the index.mustache page (template engine)

<table  class="table_col">
    <tr>
        <th>Код ошибки</th>
        <th>Описание</th>
        <th>Номер заявки(Naumen)</th>
        <th>Номер задачи (Jira)</th>
        <th>Старый номер задачи(Jira)</th>
        <th>Изображение</th>
        <th>Решение</th>
        <th>Статус</th>
        <th>Дата</th>
    </tr>
 {{#RECORD}}


<tr>
    <td bgcolor="#6759EE" id="maintd">{{tag}}</td>
    <td style="white-space:pre; ">{{description}}</td>
    <td>{{naumen}}</td>
    <td>{{jiraID}}</td>
    <td>{{oldjiraID}}</td>
    <td><a href="/downloadfile/{{id}}"><img src="/downloadfile/{{id}}"  width="180" height="180"></a></td>
    <td style="white-space:pre; ">{{resolution}}</td>
    <td >{{status}}</td>
    <td>{{date}}</td>
    <td><a href="edit/{{id}}">изменить</a></td>
    <td><a href="delete/{{id}}">удалить</a></td>
</tr>
{{/RECORD}}
</table>


Clicking on "edit" (in the table) for a record takes us to another page (editrecord.mustache).

Here I want to change some text field, but I don't want to change the picture. What code should I enter? At the same time, it should be possible to change the picture if I want it.

<div class="divtextform">
    <form action="/editrecordform" enctype="multipart/form-data" method="post"  class="contact_form">

        <input type="hidden" name="id" value="{{#editrec}}{{id}}{{/editrec}}" >
        <label for="tag">Код ошибки:</label> <input type="text" name="tag"  value="{{#editrec}}{{tag}}{{/editrec}}">
        <br>
        <label for="description">Описание:</label><textarea name="description" id="" cols="100" rows="10" wrap="hard">{{#editrec}}{{description}}{{/editrec}}</textarea>
        <br>
        <label for="naumen">Номер в Naumen:</label> <input type="text" name="naumen" value="{{#editrec}}{{naumen}}{{/editrec}}">
        <br>
        <label for="jiraID">ID в JIRA:</label> <input type="text" name="jiraID" value="{{#editrec}}{{jiraID}}{{/editrec}}">
        <br>
        <label for="oldJiraID">ID в JIRA (архив):</label> <input type="text" name="oldjiraID"  value="{{#editrec}}{{oldjiraID}}{{/editrec}}">
        <br>
        <label for="resolution">Решение:</label><textarea name="resolution" id="" cols="100" rows="10" wrap="hard"></textarea>
        <br>
        <button type="submit">изменить</button>
    </form>
</div>


Controllers:

@GetMapping("/downloadfile/{id}")
    public ResponseEntity<ByteArrayResource> downloadFile(@PathVariable Integer id){


        Piro piro = repos.findById(id).get();

        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType(piro.getDoctype()))
                .header(HttpHeaders.CONTENT_DISPOSITION,"attachment:filename=\""+piro.getDocname()+"\"")
                .body(new ByteArrayResource(piro.getData()));
    }



@GetMapping("edit/{id}")
    public  String editRecord(@PathVariable Integer id,Map <String,Object> model){

        Piro piro=repos.getOne(id);
        model.put("editrec",piro);
        return "editrecord";

    }


@PostMapping("/editrecordform")
public String getEditedInfo(@RequestParam Integer id, @RequestParam String description, @RequestParam String tag, @RequestParam String naumen, @RequestParam String jiraID, @RequestParam String oldjiraID,@RequestParam String resolution ) throws IOException {


Piro piro=repos.getOne(id);

piro.setDescription(description);
piro.setTag(tag);
piro.setNaumen(naumen);
piro.setJiraID(jiraID);
piro.setOldjiraID(oldjiraID);
piro.setResolution(resolution);


    try {

        piro.setStatus(restClient.getJiraStatus(piro.getJiraID()));

    }

    catch (HttpClientErrorException e){

        piro.setStatus("не известно");

    } catch (JSONException e) {
        e.printStackTrace();
    }

    piro.setDate(datev);
    repos.save(piro);

return "redirect:/";

    }



@PostMapping("fillform")

    public String addData(@RequestParam String description,@RequestParam String tag,@RequestParam String naumen,@RequestParam String jiraID, @RequestParam String oldJiraID, @RequestParam MultipartFile file, @RequestParam String resolution){

        try {
            Piro data = new Piro(description, tag, naumen, jiraID, oldJiraID, file.getBytes(),resolution, file.getOriginalFilename(),file.getContentType(),datev);
            try {

                    data.setStatus(restClient.getJiraStatus(data.getJiraID()));

            }

            catch (HttpClientErrorException e){

                    data.setStatus("не известно");

                }



            repos.save(data);

        }

        catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/";


    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-04-06
Hasanly @azerphoenix

How to save an image in the table when changing other fields?

And why do you delete / change the picture when changing the fields?
Let's say that there is a certain endpoint where you need to send a POST request to download a picture and it receives a MultiPartFile.
Accordingly, when loading a new picture, you can send an Ajax request to this URL, and assign the picture id to the entity that is added from the form.
This means that when you change other data, you will not change the picture in any way and, accordingly, it will not be deleted.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question