You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by eugenebalt <eu...@yahoo.com> on 2011/08/22 16:41:44 UTC

AJAXDownload Example Doesn't Support Content-Disposition (Save As)

I followed the AJAXDownload example on how to do an Ajax-based file download:

https://cwiki.apache.org/WICKET/ajax-update-and-file-download-in-one-blow.html

It works, but my problem is, I can't get it to support the
Content-Disposition header "attachment" to do the Save As dialog box. The
file opens *immediately* inside the browser even when I override
setHeader(..) for the ByteArrayResource.  This works with the regular
ResourceLink, but not this AJAXDownload.

Can anyone help how to do this AJAX-based download, but with the Save As
Content-Disposition=attachment, so it gives me a prompt before I download?

CODE:

final AJAXDownload download = new AJAXDownload()
{

	@Override
	protected IResourceStream getResourceStream() {
		// TODO Auto-generated method stub
		ByteArrayResource bar = new ByteArrayResource("application/vnd.ms-excel",
createExcelBytes());
		{
			@Override
			protected void setHeaders(WebResponse response) 			 
			{
				response.setHeader("Content-Disposition", "attachment;
filename=report.xls");
			}					
		};
		return bar.getResourceStream();
	}

};
add(download);

AjaxLink exportLink = new AjaxLink("exportLink") {

	@Override
	public void onClick(AjaxRequestTarget target) {

		download.initiate(target);
	}

};	


AJAXDownload (from example):
-----------------------------
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/AJAXDownload-Example-Doesn-t-Support-Content-Disposition-Save-As-tp3760342p3760342.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: AJAXDownload Example Doesn't Support Content-Disposition (Save As)

Posted by Igor Racic <ig...@gmail.com>.
Yes, it initialized.

Thank you,
Igor


2011/8/23 Sven Meier <sv...@meiers.net>

> Hi,
>
> are you sure proposedFileName is non-null?
>
> Sven
>
> On 08/23/2011 07:29 PM, Igor Racic wrote:
> > Hi,
> >
> > I am not able to get save dialog.
> >
> > Here is the code:
> >
> > final AJAXDownload download = new AJAXDownload()
> > {
> > @Override
> > protected IResourceStream getResourceStream()
> > {
> > return new StringResourceStream(info.srcString, "text/plain");
> > }
> >  @Override
> > protected String getFileName() {
> > return info.proposedFileName;
> > }
> >  };
> > add(download);
> >
> > add(new AjaxLink<Void>("download") {
> > @Override
> > public void onClick(AjaxRequestTarget target)
> > {
> > // do whatever with the target, e.g. refresh components
> > //target.addComponent(...);
> >
> > // finally initiate the download
> > download.initiate(target);
> > }
> > });
> >
> > Any hint to troubleshoot the problem ? (Clicking on link goes directly to
> > download)
> > Version is 1.4.17
> >
> >
> > Thank you and regards,
> > Igor
> >
> >
> >
> >
> > 2011/8/22 eugenebalt <eu...@yahoo.com>
> >
> >> Thanks I fixed it. This example explains how to do it.
> >>
> >> https://cwiki.apache.org/WICKET/best-practices-and-gotchas.html
> >>
> >>
> >>
> >> --
> >> View this message in context:
> >>
> http://apache-wicket.1842946.n4.nabble.com/AJAXDownload-Example-Doesn-t-Support-Content-Disposition-Save-As-tp3760342p3760552.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: AJAXDownload Example Doesn't Support Content-Disposition (Save As)

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

are you sure proposedFileName is non-null?

Sven

On 08/23/2011 07:29 PM, Igor Racic wrote:
> Hi,
> 
> I am not able to get save dialog.
> 
> Here is the code:
> 
> final AJAXDownload download = new AJAXDownload()
> {
> @Override
> protected IResourceStream getResourceStream()
> {
> return new StringResourceStream(info.srcString, "text/plain");
> }
>  @Override
> protected String getFileName() {
> return info.proposedFileName;
> }
>  };
> add(download);
> 
> add(new AjaxLink<Void>("download") {
> @Override
> public void onClick(AjaxRequestTarget target)
> {
> // do whatever with the target, e.g. refresh components
> //target.addComponent(...);
> 
> // finally initiate the download
> download.initiate(target);
> }
> });
> 
> Any hint to troubleshoot the problem ? (Clicking on link goes directly to
> download)
> Version is 1.4.17
> 
> 
> Thank you and regards,
> Igor
> 
> 
> 
> 
> 2011/8/22 eugenebalt <eu...@yahoo.com>
> 
>> Thanks I fixed it. This example explains how to do it.
>>
>> https://cwiki.apache.org/WICKET/best-practices-and-gotchas.html
>>
>>
>>
>> --
>> View this message in context:
>> http://apache-wicket.1842946.n4.nabble.com/AJAXDownload-Example-Doesn-t-Support-Content-Disposition-Save-As-tp3760342p3760552.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: AJAXDownload Example Doesn't Support Content-Disposition (Save As)

Posted by Igor Racic <ig...@gmail.com>.
Just to complete discussion,
right click on link didn't work with AjaxDownload version.

Regards,
Igor


2011/8/24 Igor Racic <ig...@gmail.com>

> Works by using AttributeModifier for href.
> Any objection to use it with AjaxLink ?
>
> Thank you and regards,
> Igor
>
>
> 2011/8/24 Igor Racic <ig...@gmail.com>
>
>> Hi,
>>
>> It worked in IE7. FF/Chrome option setting to download in fixed directory
>> played here. I changed to "ask on every download" and now it works (both
>> ways !).
>>
>> Is there a way so that right click propose correct name (currenctly it's
>> like xyz.htm / xyz.htm.part) ?
>>
>>
>> Thank you and regards,
>> Igor
>>
>>
>> 2011/8/23 eugenebalt <eu...@yahoo.com>
>>
>>> Igor,
>>>
>>> Don't use the AJAXDownload class. There's another way to do it via Ajax,
>>> and
>>> get the Save As dialog.
>>>
>>> Click this page:
>>> https://cwiki.apache.org/WICKET/best-practices-and-gotchas.html
>>>
>>> Go down to the example that starts with "or do it like this:" (the 2nd
>>> example in the section "Starting download after form submission (Wicket
>>> 1.2)")
>>>
>>> This example is using the WebResource.
>>>
>>> [ WebResource export = new WebResource() { ... etc. ]
>>>
>>> Use this WebResource example. Then, add a ResourceLink backed by this
>>> WebResource.
>>>
>>> I followed it directly and it works. I get the Save As... dialog box and
>>> the
>>> file bytes are constructed at the time the link is clicked.
>>>
>>> CODE:
>>> ------
>>>
>>> // Export to Spreadsheet Link
>>>
>>> // 1. Construct a WebResource from Excel bytes
>>> WebResource export = new WebResource()
>>> {
>>>        @Override
>>>        public IResourceStream getResourceStream() {
>>>                 ByteArrayResource bar = new
>>> ByteArrayResource("application/vnd.ms-excel",
>>> createExcelBytes());
>>>                 return bar.getResourceStream();
>>>         }
>>>
>>>        @Override
>>>        protected void setHeaders(WebResponse response) {
>>>                 super.setHeaders(response);
>>>                response.setAttachmentHeader("report.xls");
>>>        }
>>> };
>>> export.setCacheable(false);
>>>
>>> // 2. Add a ResourceLink based on the WebResource
>>> ResourceLink exportLink = new ResourceLink("exportLink", export);
>>>
>>> add(exportLink);
>>> ----------------------
>>>
>>>
>>> --
>>> View this message in context:
>>> http://apache-wicket.1842946.n4.nabble.com/AJAXDownload-Example-Doesn-t-Support-Content-Disposition-Save-As-tp3760342p3763726.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: AJAXDownload Example Doesn't Support Content-Disposition (Save As)

Posted by Igor Racic <ig...@gmail.com>.
Works by using AttributeModifier for href.
Any objection to use it with AjaxLink ?

Thank you and regards,
Igor


2011/8/24 Igor Racic <ig...@gmail.com>

> Hi,
>
> It worked in IE7. FF/Chrome option setting to download in fixed directory
> played here. I changed to "ask on every download" and now it works (both
> ways !).
>
> Is there a way so that right click propose correct name (currenctly it's
> like xyz.htm / xyz.htm.part) ?
>
>
> Thank you and regards,
> Igor
>
>
> 2011/8/23 eugenebalt <eu...@yahoo.com>
>
>> Igor,
>>
>> Don't use the AJAXDownload class. There's another way to do it via Ajax,
>> and
>> get the Save As dialog.
>>
>> Click this page:
>> https://cwiki.apache.org/WICKET/best-practices-and-gotchas.html
>>
>> Go down to the example that starts with "or do it like this:" (the 2nd
>> example in the section "Starting download after form submission (Wicket
>> 1.2)")
>>
>> This example is using the WebResource.
>>
>> [ WebResource export = new WebResource() { ... etc. ]
>>
>> Use this WebResource example. Then, add a ResourceLink backed by this
>> WebResource.
>>
>> I followed it directly and it works. I get the Save As... dialog box and
>> the
>> file bytes are constructed at the time the link is clicked.
>>
>> CODE:
>> ------
>>
>> // Export to Spreadsheet Link
>>
>> // 1. Construct a WebResource from Excel bytes
>> WebResource export = new WebResource()
>> {
>>        @Override
>>        public IResourceStream getResourceStream() {
>>                 ByteArrayResource bar = new
>> ByteArrayResource("application/vnd.ms-excel",
>> createExcelBytes());
>>                 return bar.getResourceStream();
>>         }
>>
>>        @Override
>>        protected void setHeaders(WebResponse response) {
>>                 super.setHeaders(response);
>>                response.setAttachmentHeader("report.xls");
>>        }
>> };
>> export.setCacheable(false);
>>
>> // 2. Add a ResourceLink based on the WebResource
>> ResourceLink exportLink = new ResourceLink("exportLink", export);
>>
>> add(exportLink);
>> ----------------------
>>
>>
>> --
>> View this message in context:
>> http://apache-wicket.1842946.n4.nabble.com/AJAXDownload-Example-Doesn-t-Support-Content-Disposition-Save-As-tp3760342p3763726.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: AJAXDownload Example Doesn't Support Content-Disposition (Save As)

Posted by Igor Racic <ig...@gmail.com>.
Hi,

It worked in IE7. FF/Chrome option setting to download in fixed directory
played here. I changed to "ask on every download" and now it works (both
ways !).

Is there a way so that right click propose correct name (currenctly it's
like xyz.htm / xyz.htm.part) ?


Thank you and regards,
Igor


2011/8/23 eugenebalt <eu...@yahoo.com>

> Igor,
>
> Don't use the AJAXDownload class. There's another way to do it via Ajax,
> and
> get the Save As dialog.
>
> Click this page:
> https://cwiki.apache.org/WICKET/best-practices-and-gotchas.html
>
> Go down to the example that starts with "or do it like this:" (the 2nd
> example in the section "Starting download after form submission (Wicket
> 1.2)")
>
> This example is using the WebResource.
>
> [ WebResource export = new WebResource() { ... etc. ]
>
> Use this WebResource example. Then, add a ResourceLink backed by this
> WebResource.
>
> I followed it directly and it works. I get the Save As... dialog box and
> the
> file bytes are constructed at the time the link is clicked.
>
> CODE:
> ------
>
> // Export to Spreadsheet Link
>
> // 1. Construct a WebResource from Excel bytes
> WebResource export = new WebResource()
> {
>        @Override
>        public IResourceStream getResourceStream() {
>                 ByteArrayResource bar = new
> ByteArrayResource("application/vnd.ms-excel",
> createExcelBytes());
>                 return bar.getResourceStream();
>         }
>
>        @Override
>        protected void setHeaders(WebResponse response) {
>                 super.setHeaders(response);
>                response.setAttachmentHeader("report.xls");
>        }
> };
> export.setCacheable(false);
>
> // 2. Add a ResourceLink based on the WebResource
> ResourceLink exportLink = new ResourceLink("exportLink", export);
>
> add(exportLink);
> ----------------------
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/AJAXDownload-Example-Doesn-t-Support-Content-Disposition-Save-As-tp3760342p3763726.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: AJAXDownload Example Doesn't Support Content-Disposition (Save As)

Posted by eugenebalt <eu...@yahoo.com>.
Igor,

Don't use the AJAXDownload class. There's another way to do it via Ajax, and
get the Save As dialog.

Click this page:
https://cwiki.apache.org/WICKET/best-practices-and-gotchas.html

Go down to the example that starts with "or do it like this:" (the 2nd
example in the section "Starting download after form submission (Wicket
1.2)")

This example is using the WebResource.

[ WebResource export = new WebResource() { ... etc. ]

Use this WebResource example. Then, add a ResourceLink backed by this
WebResource.

I followed it directly and it works. I get the Save As... dialog box and the
file bytes are constructed at the time the link is clicked.

CODE:
------

// Export to Spreadsheet Link

// 1. Construct a WebResource from Excel bytes
WebResource export = new WebResource() 
{
	@Override
	public IResourceStream getResourceStream() {
		ByteArrayResource bar = new ByteArrayResource("application/vnd.ms-excel",
createExcelBytes());
		return bar.getResourceStream();
	}

	@Override
	protected void setHeaders(WebResponse response) {
		super.setHeaders(response);
		response.setAttachmentHeader("report.xls");
	}
};
export.setCacheable(false);

// 2. Add a ResourceLink based on the WebResource
ResourceLink exportLink = new ResourceLink("exportLink", export);	

add(exportLink);
----------------------


--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/AJAXDownload-Example-Doesn-t-Support-Content-Disposition-Save-As-tp3760342p3763726.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: AJAXDownload Example Doesn't Support Content-Disposition (Save As)

Posted by Igor Racic <ig...@gmail.com>.
Hi,

I am not able to get save dialog.

Here is the code:

final AJAXDownload download = new AJAXDownload()
{
@Override
protected IResourceStream getResourceStream()
{
return new StringResourceStream(info.srcString, "text/plain");
}
 @Override
protected String getFileName() {
return info.proposedFileName;
}
 };
add(download);

add(new AjaxLink<Void>("download") {
@Override
public void onClick(AjaxRequestTarget target)
{
// do whatever with the target, e.g. refresh components
//target.addComponent(...);

// finally initiate the download
download.initiate(target);
}
});

Any hint to troubleshoot the problem ? (Clicking on link goes directly to
download)
Version is 1.4.17


Thank you and regards,
Igor




2011/8/22 eugenebalt <eu...@yahoo.com>

> Thanks I fixed it. This example explains how to do it.
>
> https://cwiki.apache.org/WICKET/best-practices-and-gotchas.html
>
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/AJAXDownload-Example-Doesn-t-Support-Content-Disposition-Save-As-tp3760342p3760552.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: AJAXDownload Example Doesn't Support Content-Disposition (Save As)

Posted by eugenebalt <eu...@yahoo.com>.
Thanks I fixed it. This example explains how to do it.

https://cwiki.apache.org/WICKET/best-practices-and-gotchas.html



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/AJAXDownload-Example-Doesn-t-Support-Content-Disposition-Save-As-tp3760342p3760552.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: AJAXDownload Example Doesn't Support Content-Disposition (Save As)

Posted by Martin Grigorov <mg...@apache.org>.
see org.apache.wicket.request.target.resource.ResourceStreamRequestTarget.configure(RequestCycle,
Response, IResourceStream)

On Mon, Aug 22, 2011 at 5:41 PM, eugenebalt <eu...@yahoo.com> wrote:
> I followed the AJAXDownload example on how to do an Ajax-based file download:
>
> https://cwiki.apache.org/WICKET/ajax-update-and-file-download-in-one-blow.html
>
> It works, but my problem is, I can't get it to support the
> Content-Disposition header "attachment" to do the Save As dialog box. The
> file opens *immediately* inside the browser even when I override
> setHeader(..) for the ByteArrayResource.  This works with the regular
> ResourceLink, but not this AJAXDownload.
>
> Can anyone help how to do this AJAX-based download, but with the Save As
> Content-Disposition=attachment, so it gives me a prompt before I download?
>
> CODE:
>
> final AJAXDownload download = new AJAXDownload()
> {
>
>        @Override
>        protected IResourceStream getResourceStream() {
>                // TODO Auto-generated method stub
>                ByteArrayResource bar = new ByteArrayResource("application/vnd.ms-excel",
> createExcelBytes());
>                {
>                        @Override
>                        protected void setHeaders(WebResponse response)
>                        {
>                                response.setHeader("Content-Disposition", "attachment;
> filename=report.xls");
>                        }
>                };
>                return bar.getResourceStream();
>        }
>
> };
> add(download);
>
> AjaxLink exportLink = new AjaxLink("exportLink") {
>
>        @Override
>        public void onClick(AjaxRequestTarget target) {
>
>                download.initiate(target);
>        }
>
> };
>
>
> AJAXDownload (from example):
> -----------------------------
> 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/AJAXDownload-Example-Doesn-t-Support-Content-Disposition-Save-As-tp3760342p3760342.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
>
>



-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com

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