62964

File download link in Tapestry

Question:

I have .tml file and its controller. There is a File variable in controller which represents an existing file from my server. I want to make this file downloadable by users, i.e. something like <a href="link-to-file">Download this file</a>. How could i do that in tapestry?

I want something like this:

//tml: <t:file source="file">Download here</t:file> //controller: private File getFile() { ... }

Answer1:

This is a simplified example which might achieve this for you.

in your tml

<a t:id="downloadLink">download</a>

in your java file

private File getFile() { ... } @Component(id="downloadLink") private ActionLink downloadLink; @OnEvent(component="downloadLink") private Object handleDownload(){ final File getFile(); final OutputStreamResponse response = new OutputStreamResponse() { public String getContentType() { return "application/pdf"; // or whatever content type your file is } public void prepareResponse(Response response) { response.setHeader ("Content-Disposition", "attachment; filename=\"" + file.getName() + "\""); } @Override public void writeToStream(OutputStream out) throws IOException { try { InputStream in = new FileInputStream(file); IOUtils.copy(in,out); in.close(); } catch (Exception e) { throw new RuntimeException(e); } } }; return response; }

Recommend

  • Angular 4 direct download images
  • PHP Explode and Get_Url: Not Showing up the URL
  • How to get the path for .exe file which resides in src/main/resources
  • How to delete files from Dropbox with PHP api
  • Netbeans: Try to load file but not found (Java)
  • Populate Multiple Forms Symfony2
  • How to setup a rewrite rule on Azure CDN to serve index.html that don't match a path pattern?
  • How to parse Java properties which contains variables?
  • How to change default timestamp that PHP uses for logging to file?
  • mysql select inside limit
  • How to create a new Bundle object?
  • EntLib Way to Bind “Null” Value to Parameter
  • Django model inheritance, filtering models
  • saving file generated by TCPDF
  • Content-Length header not returned from Pylons response
  • ActiveRecord query for a count of new users by day
  • Python urlparse: small issue
  • Is possible to count alias result on mysql
  • Cross-Platform Protobuf Serialization
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Can I make an Android app that runs a web view in Chrome 39?
  • Javascript + PHP Encryption with pidCrypt
  • When should I choose bucket sort over other sorting algorithms?
  • Hazelcast - OperationTimeoutException
  • To display the title for the current loaction in map in iphone
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • How to format a variable of double type
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Benchmarking RAM performance - UWP and C#
  • coudnt use logback because of log4j
  • How do I configure my settings file to work with unit tests?
  • Getting Messege Twice Using IMvxMessenger
  • How to stop GridView from loading again when I press back button?
  • Is it possible to post an object from jquery to bottle.py?
  • JaxB to read class hierarchy
  • How can i traverse a binary tree from right to left in java?
  • How can I use threading to 'tick' a timer to be accessed by other threads?