How to trigger a file download when clicking an HTML button or JavaScript

You can trigger a download with the HTML5 download attribute. <a href=”https://stackoverflow.com/questions/11620698/path_to_file” download=”proposed_file_name”>Download</a> Where: path_to_file is a path that resolves to an URL on the same origin. That means the page and the file must share the same domain, subdomain, protocol (HTTP vs. HTTPS), and port (if specified). Exceptions are blob: and data: (which always … Read more

Download a file from NodeJS Server using Express

Update Express has a helper for this to make life easier. app.get(‘/download’, function(req, res){ const file = `${__dirname}/upload-folder/dramaticpenguin.MOV`; res.download(file); // Set disposition and send it. }); Old Answer As far as your browser is concerned, the file’s name is just ‘download’, so you need to give it more info by using another HTTP header. res.setHeader(‘Content-disposition’, … Read more

How to download a file with Node.js (without using third-party libraries)?

You can create an HTTP GET request and pipe its response into a writable file stream: const http = require(‘http’); // or ‘https’ for https:// URLs const fs = require(‘fs’); const file = fs.createWriteStream(“file.jpg”); const request = http.get(“http://i3.ytimg.com/vi/J—aiyznGQ/mqdefault.jpg”, function(response) { response.pipe(file); // after download completed close filestream file.on(“finish”, () => { file.close(); console.log(“Download Completed”); }); … Read more

Downloading a file from spring controllers

@RequestMapping(value = “/files/{file_name}”, method = RequestMethod.GET) public void getFile( @PathVariable(“file_name”) String fileName, HttpServletResponse response) { try { // get your file as InputStream InputStream is = …; // copy it to response’s OutputStream org.apache.commons.io.IOUtils.copy(is, response.getOutputStream()); response.flushBuffer(); } catch (IOException ex) { log.info(“Error writing file to output stream. Filename was ‘{}'”, fileName, ex); throw new RuntimeException(“IOError … Read more

Returning a file to View/Download in ASP.NET MVC

public ActionResult Download() { var document = … var cd = new System.Net.Mime.ContentDisposition { // for example foo.bak FileName = document.FileName, // always prompt the user for downloading, set to true if you want // the browser to try to show the file inline Inline = false, }; Response.AppendHeader(“Content-Disposition”, cd.ToString()); return File(document.Data, document.ContentType); } NOTE: … Read more

How to force file download with PHP

Read the docs about built-in PHP function readfile $file_url=”http://www.myremoteserver.com/file.exe”; header(‘Content-Type: application/octet-stream’); header(“Content-Transfer-Encoding: Binary”); header(“Content-disposition: attachment; filename=\”” . basename($file_url) . “\””); readfile($file_url); Also make sure to add proper content type based on your file application/zip, application/pdf etc. – but only if you do not want to trigger the save-as dialog.

How to provide a file download from a JSF backing bean?

Introduction You can get everything through ExternalContext. In JSF 1.x, you can get the raw HttpServletResponse object by ExternalContext#getResponse(). In JSF 2.x, you can use the bunch of new delegate methods like ExternalContext#getResponseOutputStream() without the need to grab the HttpServletResponse from under the JSF hoods. On the response, you should set the Content-Type header so … Read more