You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by MattyDE <uf...@gmail.com> on 2010/07/15 09:46:37 UTC

Initiating File Download through Ajax is not working

Hi Folks,

i need to write an Web-XML-Filler. So i have to generate a XML-File
on-the-fly and after that i want to stream the generated File to the
browser.

Iam using this Example :
https://cwiki.apache.org/confluence/display/WICKET/AJAX+update+and+file+download+in+one+blow 
... and it works half with the behavoir.

                download = new AjaxDownloadBehavoir()
		{
			@Override
			protected IResourceStream getResourceStream()
			{	
				return new FileResourceStream(getXMLFile());
			}
		};
		add(download);

After initiating the File is not streamed to the browser, no, its shown in
the browser as a text-page.

btw: Iam Using an AjaxButton (with a form) not an AjaxLink... does this
matter?

Any hints?

Thanks in Advance!
-- 
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Initiating-File-Download-through-Ajax-is-not-working-tp2289784p2289784.html
Sent from the Wicket - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Initiating File Download through Ajax is not working

Posted by "Rain... Is wet!" <or...@hotmail.de>.
I'm using the AJAXDownload with success in my current project (Wicket 1.5.4).

Just create an instance of the AJAXDownload, override getResourceStream to
return your IResourceStream and getFileName (would recommend it) to return
your desired filename.

When done, add it to your AJAXSubmit component (AjaxLink, AjaxFallbackLink
or AjaxButton... or similar) and initiate your download in its onSubmit
method via "yourAJAXDownload.initiate(yourIResourceStream);".

Basically its 100% like in the example given here:
https://cwiki.apache.org/WICKET/ajax-update-and-file-download-in-one-blow.html

-----
Never forget your umbrella ! In case You did, try to find a shelter ;)
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Initiating-File-Download-through-Ajax-is-not-working-tp2289784p4469023.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Initiating File Download through Ajax is not working

Posted by MattyDE <uf...@gmail.com>.
I updated to Wicket 1.4.19  now but the FileDownload does not work anymore.

The user get redirected to a certain URL but no Dowload-Stream starts.

Any hint to fix this issue?


Thanks!

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Initiating-File-Download-through-Ajax-is-not-working-tp2289784p4127552.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Initiating File Download through Ajax is not working

Posted by Ernesto Reinaldo Barreiro <re...@gmail.com>.
Not sure there is a way to overcome that limitation without modifying
browser options. Maybe then it is safer to use another approach like
use AJAX to present a "floating dialog" with a "normal" download link.
 Users will need one more click but hat will certainly work for all
browsers and/or settings.

Regards,

Ernesto

On Mon, Dec 6, 2010 at 5:30 PM, Jonathan Proulx
<Jo...@bluberi.com> wrote:
> Hello
>
> In the AJAXDownload class, the appended javascript "window.location.href..." will make the browser display a warning on top to prevent file download.
> If I select the warning then try to download the file, it doesn't work, the file download dialog never popup.
> The second time I trigger the download it works.
>
> Anybody knowns a way to avoid browser warning without modifying browser options?
>
> Or is there another way to initiate file download through ajax?
>
> Thanks
> Jonathan
>
>
> Jonathan Proulx, Programmer Analyst /
>
> bluberi gaming technologies inc.
> Drummondville, Quebec Canada
> 819.475.5155 (telephone) 819.475.5156 (fax) bluberi.com
>
> --------------------------------------------------------------------------
> This e-mail and any attachments transmitted with it are personal, privileged and confidential and solely for the use of the individual to whom they are addressed and intended. If you have received this e-mail in error, please notify the sender by return e-mail. If you are not the intended recipient or if you believe that you are not, you are hereby notified that the dissemination, distribution, plagiarism or copying of this e-mail and attachments transmitted with it is strictly prohibited.
>
> Ce courriel et les pièces jointes s’y rattachant sont de nature personnelle, privilégiée et confidentielle et pour l’usage exclusif du destinataire à qui ils sont adressés et destinés. Si vous avez reçu ce courriel par erreur, veuillez s’il vous plaît le renvoyer à l’expéditeur. Si vous n’êtes pas le bon destinataire ou si vous croyez ne pas l’être, nous vous informons immédiatement que la publication, distribution, diffusion, copie ou plagiat de ce courriel et de ses pièces jointes sont strictement interdits.
> --------------------------------------------------------------------------
> -----Message d'origine-----
>
>
> De : val360 [mailto:val_wicket@360works.com]
> Envoyé : 3 décembre 2010 16:42
> À : users@wicket.apache.org
> Objet : Re: Initiating File Download through Ajax is not working
>
>
> final AJAXDownload download = new AJAXDownload() {
>  @Override
>  protected IResourceStream getResourceStream() {
>        return new FileResourceStream(theDatabase.getFile());
>  }
>
>  @Override
>  protected String getFileName() {
>        return theDatabase.getFile().getAbsoluteFile().getName();  //FIX!!! the
> filename gets .html appended to it.
>  }
> };
> add(download);
>
> add( new AjaxLink("download") {
>        public void onClick( AjaxRequestTarget target ) {
>                if( theDatabase.isOpen() ) {
>                        target.appendJavascript( "alert('The database is open, close or pause the
> db first.');" );
>                } else {
>                        download.initiate( target );
>                }
>        }
> });
>
> And this class is unchanged:
>
> import org.apache.wicket.ajax.AjaxRequestTarget;
> import org.apache.wicket.behavior.AbstractAjaxBehavior;
> import
> org.apache.wicket.request.target.resource.ResourceStreamRequestTarget;
> import org.apache.wicket.util.resource.IResourceStream;
>
> /**
>  * @author Sven Meier
>  * @author Ernesto Reinaldo Barreiro (reiern70@gmail.com)
>  */
> public abstract class AJAXDownload extends AbstractAjaxBehavior
> {
>        /**
>         * Call this method to initiate the download.
>         */
>        public void initiate(AjaxRequestTarget target)
>        {
>                CharSequence url = getCallbackUrl();
>
>                target.appendJavascript("window.location.href='" + url + "'");
>        }
>
>        public void onRequest()
>        {
>                getComponent().getRequestCycle().setRequestTarget(
>                                new ResourceStreamRequestTarget(getResourceStream(), getFileName()));
>        }
>
>        /**
>         * @see ResourceStreamRequestTarget#getFileName()
>         */
>        protected String getFileName()
>        {
>                return null;
>        }
>
>        /**
>         * Hook method providing the actual resource stream.
>         */
>        protected abstract IResourceStream getResourceStream();
> }
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/Initiating-File-Download-through-Ajax-is-not-working-tp2289784p3071816.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


RE: Initiating File Download through Ajax is not working

Posted by Jonathan Proulx <Jo...@bluberi.com>.
Hello

In the AJAXDownload class, the appended javascript "window.location.href..." will make the browser display a warning on top to prevent file download.
If I select the warning then try to download the file, it doesn't work, the file download dialog never popup. 
The second time I trigger the download it works.

Anybody knowns a way to avoid browser warning without modifying browser options?

Or is there another way to initiate file download through ajax?

Thanks
Jonathan


Jonathan Proulx, Programmer Analyst / 

bluberi gaming technologies inc.
Drummondville, Quebec Canada
819.475.5155 (telephone) 819.475.5156 (fax) bluberi.com

--------------------------------------------------------------------------
This e-mail and any attachments transmitted with it are personal, privileged and confidential and solely for the use of the individual to whom they are addressed and intended. If you have received this e-mail in error, please notify the sender by return e-mail. If you are not the intended recipient or if you believe that you are not, you are hereby notified that the dissemination, distribution, plagiarism or copying of this e-mail and attachments transmitted with it is strictly prohibited.

Ce courriel et les pièces jointes s’y rattachant sont de nature personnelle, privilégiée et confidentielle et pour l’usage exclusif du destinataire à qui ils sont adressés et destinés. Si vous avez reçu ce courriel par erreur, veuillez s’il vous plaît le renvoyer à l’expéditeur. Si vous n’êtes pas le bon destinataire ou si vous croyez ne pas l’être, nous vous informons immédiatement que la publication, distribution, diffusion, copie ou plagiat de ce courriel et de ses pièces jointes sont strictement interdits.
--------------------------------------------------------------------------
-----Message d'origine-----


De : val360 [mailto:val_wicket@360works.com] 
Envoyé : 3 décembre 2010 16:42
À : users@wicket.apache.org
Objet : Re: Initiating File Download through Ajax is not working


final AJAXDownload download = new AJAXDownload() {
  @Override
  protected IResourceStream getResourceStream() {
	return new FileResourceStream(theDatabase.getFile());
  }

  @Override
  protected String getFileName() {
	return theDatabase.getFile().getAbsoluteFile().getName();  //FIX!!! the
filename gets .html appended to it.
  }
};
add(download);

add( new AjaxLink("download") {
	public void onClick( AjaxRequestTarget target ) {
		if( theDatabase.isOpen() ) {
			target.appendJavascript( "alert('The database is open, close or pause the
db first.');" );
		} else {
			download.initiate( target );
		}
	}
});

And this class is unchanged:

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.behavior.AbstractAjaxBehavior;
import
org.apache.wicket.request.target.resource.ResourceStreamRequestTarget;
import org.apache.wicket.util.resource.IResourceStream;

/**
 * @author Sven Meier
 * @author Ernesto Reinaldo Barreiro (reiern70@gmail.com)
 */
public abstract class AJAXDownload extends AbstractAjaxBehavior
{
	/**
	 * Call this method to initiate the download.
	 */
	public void initiate(AjaxRequestTarget target)
	{
		CharSequence url = getCallbackUrl();

		target.appendJavascript("window.location.href='" + url + "'");
	}

	public void onRequest()
	{
		getComponent().getRequestCycle().setRequestTarget(
				new ResourceStreamRequestTarget(getResourceStream(), getFileName()));
	}
	
	/**
	 * @see ResourceStreamRequestTarget#getFileName()
	 */
	protected String getFileName()
	{
		return null;
	}

	/**
	 * Hook method providing the actual resource stream.
	 */
	protected abstract IResourceStream getResourceStream();
}
-- 
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Initiating-File-Download-through-Ajax-is-not-working-tp2289784p3071816.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Initiating File Download through Ajax is not working

Posted by Sven Meier <sv...@meiers.net>.
Wicket sets the content type for you *if*
- it is provided by the resource stream
- can be looked up via servlet context from web.xml
- can be derived from URLConnection.

See Response#detectContentType(). Please try out the first option, i.e. override FileResourceStream#getContentType().

Sven

Am 04.12.2010 um 16:57 schrieb val360:

> 
> Yes it does, it's .fp7
> 
> Which is not an extension web server would recognize, could that be the
> issue?
> -- 
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/Initiating-File-Download-through-Ajax-is-not-working-tp2289784p3072514.html
> Sent from the Users forum mailing list archive at Nabble.com.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Initiating File Download through Ajax is not working

Posted by val360 <va...@360works.com>.
Yes it does, it's .fp7

Which is not an extension web server would recognize, could that be the
issue?
-- 
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Initiating-File-Download-through-Ajax-is-not-working-tp2289784p3072514.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Initiating File Download through Ajax is not working

Posted by Sven Meier <sv...@meiers.net>.
Looks fine to me. Does you filename have an extension (e.g. ".txt")?

Sven

On Fri, 2010-12-03 at 13:42 -0800, val360 wrote:
> final AJAXDownload download = new AJAXDownload() {
>   @Override
>   protected IResourceStream getResourceStream() {
> 	return new FileResourceStream(theDatabase.getFile());
>   }
> 
>   @Override
>   protected String getFileName() {
> 	return theDatabase.getFile().getAbsoluteFile().getName();  //FIX!!! the
> filename gets .html appended to it.
>   }
> };
> add(download);
> 
> add( new AjaxLink("download") {
> 	public void onClick( AjaxRequestTarget target ) {
> 		if( theDatabase.isOpen() ) {
> 			target.appendJavascript( "alert('The database is open, close or pause the
> db first.');" );
> 		} else {
> 			download.initiate( target );
> 		}
> 	}
> });
> 
> And this class is unchanged:
> 
> import org.apache.wicket.ajax.AjaxRequestTarget;
> import org.apache.wicket.behavior.AbstractAjaxBehavior;
> import
> org.apache.wicket.request.target.resource.ResourceStreamRequestTarget;
> import org.apache.wicket.util.resource.IResourceStream;
> 
> /**
>  * @author Sven Meier
>  * @author Ernesto Reinaldo Barreiro (reiern70@gmail.com)
>  */
> public abstract class AJAXDownload extends AbstractAjaxBehavior
> {
> 	/**
> 	 * Call this method to initiate the download.
> 	 */
> 	public void initiate(AjaxRequestTarget target)
> 	{
> 		CharSequence url = getCallbackUrl();
> 
> 		target.appendJavascript("window.location.href='" + url + "'");
> 	}
> 
> 	public void onRequest()
> 	{
> 		getComponent().getRequestCycle().setRequestTarget(
> 				new ResourceStreamRequestTarget(getResourceStream(), getFileName()));
> 	}
> 	
> 	/**
> 	 * @see ResourceStreamRequestTarget#getFileName()
> 	 */
> 	protected String getFileName()
> 	{
> 		return null;
> 	}
> 
> 	/**
> 	 * Hook method providing the actual resource stream.
> 	 */
> 	protected abstract IResourceStream getResourceStream();
> }



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Initiating File Download through Ajax is not working

Posted by val360 <va...@360works.com>.
final AJAXDownload download = new AJAXDownload() {
  @Override
  protected IResourceStream getResourceStream() {
	return new FileResourceStream(theDatabase.getFile());
  }

  @Override
  protected String getFileName() {
	return theDatabase.getFile().getAbsoluteFile().getName();  //FIX!!! the
filename gets .html appended to it.
  }
};
add(download);

add( new AjaxLink("download") {
	public void onClick( AjaxRequestTarget target ) {
		if( theDatabase.isOpen() ) {
			target.appendJavascript( "alert('The database is open, close or pause the
db first.');" );
		} else {
			download.initiate( target );
		}
	}
});

And this class is unchanged:

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.behavior.AbstractAjaxBehavior;
import
org.apache.wicket.request.target.resource.ResourceStreamRequestTarget;
import org.apache.wicket.util.resource.IResourceStream;

/**
 * @author Sven Meier
 * @author Ernesto Reinaldo Barreiro (reiern70@gmail.com)
 */
public abstract class AJAXDownload extends AbstractAjaxBehavior
{
	/**
	 * Call this method to initiate the download.
	 */
	public void initiate(AjaxRequestTarget target)
	{
		CharSequence url = getCallbackUrl();

		target.appendJavascript("window.location.href='" + url + "'");
	}

	public void onRequest()
	{
		getComponent().getRequestCycle().setRequestTarget(
				new ResourceStreamRequestTarget(getResourceStream(), getFileName()));
	}
	
	/**
	 * @see ResourceStreamRequestTarget#getFileName()
	 */
	protected String getFileName()
	{
		return null;
	}

	/**
	 * Hook method providing the actual resource stream.
	 */
	protected abstract IResourceStream getResourceStream();
}
-- 
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Initiating-File-Download-through-Ajax-is-not-working-tp2289784p3071816.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Initiating File Download through Ajax is not working

Posted by Ernesto Reinaldo Barreiro <re...@gmail.com>.
Show your code?

Ernesto

On Fri, Dec 3, 2010 at 7:06 PM, val360 <va...@360works.com> wrote:
>
> Hi
>
> It's mostly working for me as well, except that the downloaded file has
> .html appended to it's name.  Any hints?
>
> -Val
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/Initiating-File-Download-through-Ajax-is-not-working-tp2289784p3071504.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Initiating File Download through Ajax is not working

Posted by val360 <va...@360works.com>.
Hi

It's mostly working for me as well, except that the downloaded file has
.html appended to it's name.  Any hints?

-Val
-- 
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Initiating-File-Download-through-Ajax-is-not-working-tp2289784p3071504.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Initiating File Download through Ajax is not working

Posted by MattyDE <uf...@gmail.com>.
Hey Svaen, thanks a lot. 

This works smoothly ;)

                download = new AjaxDownloadBehavoir()
		{
			@Override
			protected IResourceStream getResourceStream()
			{	
				return new FileResourceStream(getXMLFile());
			}
			
			@Override
			protected String getFileName() {
				return getXMLFile().getAbsoluteFile().getName();
			}
		};
-- 
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Initiating-File-Download-through-Ajax-is-not-working-tp2289784p2289809.html
Sent from the Wicket - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Initiating File Download through Ajax is not working

Posted by Sven Meier <sv...@meiers.net>.
Hi,

IIRC you have to override AJAXDownload#getFileName(), this should 
trigger a download dialog in the browser.

Regards

Sven

On 07/15/2010 09:46 AM, MattyDE wrote:
> Hi Folks,
>
> i need to write an Web-XML-Filler. So i have to generate a XML-File
> on-the-fly and after that i want to stream the generated File to the
> browser.
>
> Iam using this Example :
> https://cwiki.apache.org/confluence/display/WICKET/AJAX+update+and+file+download+in+one+blow
> ... and it works half with the behavoir.
>
>                  download = new AjaxDownloadBehavoir()
> 		{
> 			@Override
> 			protected IResourceStream getResourceStream()
> 			{	
> 				return new FileResourceStream(getXMLFile());
> 			}
> 		};
> 		add(download);
>
> After initiating the File is not streamed to the browser, no, its shown in
> the browser as a text-page.
>
> btw: Iam Using an AjaxButton (with a form) not an AjaxLink... does this
> matter?
>
> Any hints?
>
> Thanks in Advance!
>    


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org