Update a file input

Published 6 months ago by mcardosob

Hello everyone

I have a CRUD controller (ClientController). In my create action I create a client with a multiple attachment file.

For this, I have a table named "client_attachments" with a id_client and the path for locate the file.

The problem is that I want to create a update action. I want that in the update view the user can delete the attachements or add a new attachment.

For this I was thinking create a javascript array when the client delete an existing attachement, the id of attachment is stocked in this array.

Like this when the client send the form, my javascript send the array (containing all id of attachments to delete) to my update action for delete the correspondant attachments with id passed.

I want to know if this methodology is the best practise?

If it is the best practise, I want to pass my javascript array to my controller jointly to my other fields presents in my request?

I hope I am obvious (I'm not the best english writter, thanks for yours understanding).

Best Answer (As Selected By mcardosob)
Dry7

@mcardosob I usually use this practice. Or you can delete the file immediately after clicking on the button, with a separate ajax query.

Dry7
Dry7
6 months ago (152,030 XP)

@mcardosob I usually use this practice. Or you can delete the file immediately after clicking on the button, with a separate ajax query.

mcardosob

@Dry7 this is a good suggestion, but I don't want delete immediately the attachment. I prefere do this when the user send the form. For this I have a ajax request to get the array "attachments_to_delete" in my controller. It is the good syntax ?

$.ajax({
    type: "PUT",
    url: "/client/3",
    data: attachments_to_delete,
    contentType: "json",
    success:function(data) {
        concsole.log(data.message)
    } 
});

Thank your for your answer.

Dry7
Dry7
6 months ago (152,030 XP)

@mcardosob Yes. only usually not only the list of deleted images is transferred to the server, but also the other data.

$.ajax({
    type: "PUT",
    url: "/client/3",
    data: {name: name, attachments_to_delete: attachments_to_delete},
    contentType: "json",
    success:function(data) {
        concsole.log(data.message)
    } 
});

Please sign in or create an account to participate in this conversation.