I cannot remove element from the list

Asked

Viewed 86 times

0

When I upload photos using FileUpload I can’t delete from the list where I’m adding.

Use that StremedContent to be listed contentNova every time the method is called by FileUpload. The problem is when I delete the photo just disappears from (``datagrid) as shown below, and is not downgraded from the list contentNova, with that, in the methods salvarFotoNova() and criaArquivo() all photos in the list are written to the server folder contentNova included those I tried to delete. inserir a descrição da imagem aqui

If I try to delete some of these 3 images, it will leave there, more will not leave the list where I am adding(contentNova), and will then be saved to the system folder when the save button is clicked.

inserir a descrição da imagem aqui

as shown in this image, I tried to delete a photo from the previous image, but it was still written in the folder.

Xhtml

  <div class="col-md-6">
 <p:outputLabel value="Foto Atual " style="font-size: 17px;" />
     <div class="row">
     <p:fileUpload  label="Anexar Foto" 
        fileUploadListener="#{ordemServicoMB.uploadNova}" auto="true"
        mode="advanced" sizeLimit="3145728" 
        multiple="true" update="gridfotosNova" allowTypes="/(\.|\/)(gif|jpg|png|JPG|bmp|jpeg)$/"  />   
     </div>
  <p:dataGrid id="gridfotosNova" value="#{ordemServicoMB.contentNova}" var="linfotosNova" columns="1" emptyMessage="">
     <p:column>
        <p:graphicImage value="#{linfotosNova.stream}" />
     </p:column>                                                                                                                                                                                                                                                                                                                                                                                  
     <p:column>
         <h:outputText  value="#{linfotosNova.name}"  />
     </p:column>                                                                                                                                                                                                                                                                                                                                                                                  
  </p:dataGrid>

Fotomb.JAVA

private List<StreamedContent> contentNova = new ArrayList<>();    
public void uploadNova(FileUploadEvent evento) {
try {

    contentNova.add(new DefaultStreamedContent(evento.getFile().getInputstream(), evento.getFile().getFileName()));

} catch (IOException erro) {
    erro.printStackTrace();
    FacesUtils.showMessage(3, "Falha em carregar imagem");
}
 }

 public void salvarFotoNova() {
    for (StreamedContent s : contentNova) {
        String nomeArquivo = s.getContentType();
        criaArquivo(nomeArquivo.getBytes(), nomeArquivo);
        }
}

 public void criaArquivo(byte[] bytes, String arquivo) {
try {
    FileOutputStream fos;
    fos = new FileOutputStream(arquivo);
    fos.write(bytes);
    fos.close();
} catch (Exception ex) {
    ex.printStackTrace();
    FacesUtils.showMessage(3, "Falha ao escrever imagem");
}
}
  • 1

    Dude, I don’t understand anything you wrote, check out your post.

  • places a more descriptive title so that other Stackoverflow colleagues can also help you

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.