Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Cancel download method when the user cancel the download file

661413Jan 6 2009 — edited Jan 9 2009
I have a method that download a file ("downloadLog_action()")when the user click over the comandlink component and the dialog box is opening to save the file, but how to know that the user canceled the download file to cancel all accions.

This is the code of the commandlink in the jspx page that call the method and pass one parameter:

<af:commandLink binding="#{ConsultaValidacion.commandLink1}"
id="commandLink1"
action="#{ConsultaValidacion.descargarLog_action}"
text="Log">
<f:param name="codInstitucion"
value="#{row.CodInstitucion}"
binding="#{ConsultaValidacion.codInstitucion}"
id="codInstitucion"/>
</af:commandLink>

This is the method to download the file:

HttpServletResponse response = (HttpServletResponse)fctx.getExternalContext().getResponse();
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachement; filename=\"Log_" +
codigoInstitucion + ".txt\"");
response.setContentLength((new Long(blob.getLength())).intValue());

//Streams de entrada y salida
InputStream is;
OutputStream out;

try {
//Obtenemos el stream de entrada el cual se lo extrae de la BD
is = blob.getInputStream();
//Obtenemos el stream de salida para el cliente
out = response.getOutputStream();

//Sección de código que me permite escribir los datos en el stream de salida
int size = (new Long(blob.getLength())).intValue();
byte[] buffer = new byte[size];
int length = -1;
while ((length = is.read(buffer)) != -1) {
out.write(buffer, 0, length);
}

//Cerramos los streams
out.flush();
is.close();
out.close();

*//Una vez descargado todo el LOG actualizamos el contador. HERE IS THE PROBLEM BECAUSE THE COUNTER IS UPDATING WHEN THE USER CANCEL THE DOWNLOAD!!! AND ITS WRONG*
row.setNumDescargaLog(new Number(num + 1));

//Guardamos los cambios
webRvc.getAppModuleWebRvc().getDBTransaction().commit();

//Request lifecyce understands that the response is completed and no further action needs to happen
fctx.responseComplete();

} catch (IOException e) {
e.printStackTrace();
webRvc.close("Consulta bitacoras validacion");
return null;
} catch (JboException ex) {
webRvc.getAppModuleWebRvc().getDBTransaction().rollback();
webRvc.close("Consulta bitacoras validacion");
return null;
}


This works fine.

The problem occurs when the user cancels the download, because all the sentences run including updating the counter downloads, the counter only to be updated only when the user chooses to save the file.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 6 2009
Added on Jan 6 2009
2 comments
402 views