File Download Dialog mit JSF

katamshut

Grünschnabel
Hallo

Ich habe ein Problem mit dem File Download Dialog Fenster welches nach einem Klick auf meinen Link angezeigt werden soll.

Ich verwende JSF (MyFaces) mit JSP und AJAX.

Das Ziel:

Ich habe einen Link auf einer Seite, welcher ein Dialog Fenster öffnet zum speichern der dahinter liegenden Datei.

Momentaner Zustand:

Ich klicke auf meinen Link und der Inhalt des Files wird mir im Browser dargestellt. Kein Dialog wird geöffnet (IE6,FF2).

Mein JSP Code:
Code:
<a4j:commandLink
	value="download von selektierte daten"
	title="Suche nach angegebenem Datum"
	action="#{LoggingBean.logFileUpload}">
	<f:param value="test.pdf" name="document"/>
</a4j:commandLink>

Mein Java Code:
Code:
	public String logFileUpload()
{
	System.out.println("jetzt");
	String DOCUMENTS_LOCATION = "c:/temp/documents/";
	FacesContext fctx = FacesContext.getCurrentInstance();
	// read documentName name from the request
	ValueBinding documentParameter = fctx.getApplication().createValueBinding("#{param.document}");
	Object documentName = documentParameter.getValue(fctx);
	if (documentName != null) {
		File srcdoc = new File(DOCUMENTS_LOCATION + (String) documentName);
		if (srcdoc.exists()) {
			FileInputStream fis;
			byte[] b;
			HttpServletResponse response = (HttpServletResponse) fctx.getExternalContext().getResponse();
			// file should be downloaded without display
			response.setContentType("application/x-download");
			response.setHeader("Content-disposition","attachement; filename=\"" + documentName + "\"");
			response.setContentLength((new Long(srcdoc.length())).intValue());
			OutputStream out;
			try {
				out = response.getOutputStream();
			} catch (IOException e) {
				// TODO
				e.printStackTrace();
				// no error message shown to the user. Consider adding a Faces
				// Message
				return null;
			}
			try {
				fis = new FileInputStream(srcdoc);
				int n;
				while ((n = fis.available()) > 0) {
					b = new byte[n];
					int result = fis.read(b);
					out.write(b, 0, b.length);
					if (result == -1)
						break;
				}
			}
			catch (IOException e)
			{
				e.printStackTrace();
				return null;
			}
			try {
				out.flush();
				out.close();
			} catch (IOException e) {
				// TODO
			}
			fctx.responseComplete();
		}
	}
	return null;
}

Hat jemand Erfahrung mit diesem Problem oder Verbesserungsvorschläge?
 
Zuletzt bearbeitet:
Vielleicht noch was zur Ergänzung:

Die Daten werden im Browser dargestellt (löscht den screen und stellt alle daten des Files dar...).
 

Neue Beiträge

Zurück