When I upload photos using FileUpload
, I can not delete from the list where I am adding.
I use this StremedContent
to be set in the contentNova
list every time the method is called by FileUpload
. The problem is when I delete the photo just add ('' dataGrid '') as shown below, and it is not decremented from the contentNova
list, so in the salvarFotoNova()
and criaArquivo()
methods are written to the server folder all photos which exists in the contentNova
list that I tried to exclude.
IfItrytodeletesomeofthese3images,itwillexit,butitwillnotexitthelistwhereIamadding(contentNova
),andconsequentlyitwillbesavedinthesystemfolderwhenthesavebuttonisclicked.p>
Asshowninthisimage,Itriedtodeleteaphotofromthepreviousimage,eventhoughitwaswrittentothefolder.
Xhtml
<divclass="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");
}
}