Downloading files
Thursday, 21 August 2014 · 4 messages
LJ
Lars Johnsen21 Aug 2014 15:20 #
I am using BaseX restxq for accessing a repository from a web browser. Uploading files works smoothly, but I can't see how to make a download button work.
For uploading, the recipie on the restxq help page was enough to get it to work. Is there a corresponding way for making downloading work? What I have tried is to let BaseX send a html-page containing:
<form method="get" action="/download/{$file}">
<button type="submit">Download</button>
</form>To process this form is the following restfunction
declare
%rest:path("/download/{$file}")
%output:method("html")
function page:download-file($file) { ... }
Inside the curly braces, I have tried an <a href..> element and
file:read-binary, but none of them with any success. BaseX complains about the <a> element, and file:read-binary outputs directly to the browser.
Any suggestions?LJ
Lars Johnsen21 Aug 2014 16:40 #
I came a little closer by making custom http:headers, but I have to confess I'm in deep water here:
declare
%rest:path("/download/{$file}")
function page:download-file($file)
{
<rest:response>
<http:response status="200" message="OK">
<http:header name="Content-Disposition" value="Attachment"/>
<http:header name="filename" value="{$file}"/>
</http:response>
</rest:response> };
This function do trigger a download of a file with the appropriate file name (=$file) containing the text OK. If I just could find somewhere in this code to put the contents of file, it should solve the problem.
Best,
Lars
2014-08-21 15:20 GMT+02:00 Lars Johnsen <yoonsen at gmail.com>:
Quoted text · Lars Johnsen · 26 lines
> I am using BaseX restxq for accessing a repository from a web browser. > Uploading files works smoothly, but I can't see how to make a download > button work. > For uploading, the recipie on the restxq help page was enough to get it to > work. Is there a corresponding way for making downloading work? > What I have tried is to let BaseX send a html-page containing: > > <form method="get" action="/download/{$file}"> > <button type="submit">Download</button> > </form> > > To process this form is the following restfunction > > declare > %rest:path("/download/{$file}") > %output:method("html") > function page:download-file($file) { ... } > > Inside the curly braces, I have tried an <a href..> element and > file:read-binary, but none of them with any success. BaseX complains about > the <a> element, and file:read-binary outputs directly to the browser. > > Any suggestions? > > >
AB
Andy Bunce21 Aug 2014 17:10 #
Hi Lars,
You need to return a sequence of two items: (restxq:response,thedata) I do something like...
declare
%rest:path("/download/{$file}")
function page:download-file($file)
{
(download-response("raw",$file), file:read-binary(..))
};
(:~ headers for download :)
declare function download-response($method,$filename){
<restxq:response>
<output:serialization-parameters>
<output:method value="{$method}"/>
</output:serialization-parameters>
<http:response>
<http:header name="Content-Disposition"
value='attachment;filename="{$filename}"'/>
</http:response>
</restxq:response>
};/Andy
Quoted text · Lars Johnsen · 55 lines
On 21 August 2014 15:40, Lars Johnsen <yoonsen at gmail.com> wrote: > I came a little closer by making custom http:headers, but I have to > confess I'm in deep water here: > > declare > %rest:path("/download/{$file}") > function page:download-file($file) > { > <rest:response> > <http:response status="200" message="OK"> > > <http:header name="Content-Disposition" value="Attachment"/> > <http:header name="filename" value="{$file}"/> > </http:response> > </rest:response> > > }; > > This function do trigger a download of a file with the appropriate file > name (=$file) containing the text OK. If I just could find somewhere in > this code to put the contents of file, it should solve the problem. > > Best, > Lars > > > 2014-08-21 15:20 GMT+02:00 Lars Johnsen <yoonsen at gmail.com>: > > I am using BaseX restxq for accessing a repository from a web browser. >> Uploading files works smoothly, but I can't see how to make a download >> button work. >> For uploading, the recipie on the restxq help page was enough to get it >> to work. Is there a corresponding way for making downloading work? >> What I have tried is to let BaseX send a html-page containing: >> >> <form method="get" action="/download/{$file}"> >> <button type="submit">Download</button> >> </form> >> >> To process this form is the following restfunction >> >> declare >> %rest:path("/download/{$file}") >> %output:method("html") >> function page:download-file($file) { ... } >> >> Inside the curly braces, I have tried an <a href..> element and >> file:read-binary, but none of them with any success. BaseX complains about >> the <a> element, and file:read-binary outputs directly to the browser. >> >> Any suggestions? >> >> >> >
LJ
Lars Johnsen21 Aug 2014 17:31 #
Thanks a lot Andy! It works like a charm. Now the filename can be manipulated as well. Perfect!
Best,
Lars
2014-08-21 17:10 GMT+02:00 Andy Bunce <bunce.andy at gmail.com>:
Quoted text · Andy Bunce · 85 lines
> > Hi Lars, > You need to return a sequence of two items: (restxq:response,thedata) > I do something like... > > declare > %rest:path("/download/{$file}") > function page:download-file($file) > { > (download-response("raw",$file), file:read-binary(..)) > }; > > (:~ headers for download :) > declare function download-response($method,$filename){ > <restxq:response> > <output:serialization-parameters> > <output:method value="{$method}"/> > </output:serialization-parameters> > <http:response> > <http:header name="Content-Disposition" > value='attachment;filename="{$filename}"'/> > </http:response> > </restxq:response> > }; > > /Andy > > > On 21 August 2014 15:40, Lars Johnsen <yoonsen at gmail.com> wrote: > >> I came a little closer by making custom http:headers, but I have to >> confess I'm in deep water here: >> >> declare >> %rest:path("/download/{$file}") >> function page:download-file($file) >> { >> <rest:response> >> <http:response status="200" message="OK"> >> >> <http:header name="Content-Disposition" value="Attachment"/> >> <http:header name="filename" value="{$file}"/> >> </http:response> >> </rest:response> >> >> }; >> >> This function do trigger a download of a file with the appropriate file >> name (=$file) containing the text OK. If I just could find somewhere in >> this code to put the contents of file, it should solve the problem. >> >> Best, >> Lars >> >> >> 2014-08-21 15:20 GMT+02:00 Lars Johnsen <yoonsen at gmail.com>: >> >> I am using BaseX restxq for accessing a repository from a web browser. >>> Uploading files works smoothly, but I can't see how to make a download >>> button work. >>> For uploading, the recipie on the restxq help page was enough to get it >>> to work. Is there a corresponding way for making downloading work? >>> What I have tried is to let BaseX send a html-page containing: >>> >>> <form method="get" action="/download/{$file}"> >>> <button type="submit">Download</button> >>> </form> >>> >>> To process this form is the following restfunction >>> >>> declare >>> %rest:path("/download/{$file}") >>> %output:method("html") >>> function page:download-file($file) { ... } >>> >>> Inside the curly braces, I have tried an <a href..> element and >>> file:read-binary, but none of them with any success. BaseX complains about >>> the <a> element, and file:read-binary outputs directly to the browser. >>> >>> Any suggestions? >>> >>> >>> >> >
Written for BaseX 7.9. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery