Hi all,

I am giving the href to give a link to a file.
The problem is that in IE it opens the text files in the same browser, but in mozilla it works fine(gives prompt of save as option).


I want the same functionality for IE also, searching Google I found so much solution but all were saying about writing php.

I don't want to implement php, can it be done simply using by HTML,jsp and struts tag.

Regards,

Dani AI

Generated

Short answer: do it server-side. Client tricks like using target or telling the user to right‑click only change presentation or rely on user action; they won't reliably force Internet Explorer to show a Save/Save As dialog. and covered those UX workarounds, suggested zipping, and 's PHP example demonstrates the server approach. If you don't want PHP, implement the same idea from a servlet/JSP (or Struts stream result): set the response headers and stream the file.

Example servlet approach (safe-ish, simple streaming and headers):

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    String name = req.getParameter("file");
    File f = new File(getServletContext().getRealPath("/files/"), name);
    if (!f.exists()) { resp.sendError(HttpServletResponse.SC_NOT_FOUND); return; }

    resp.setContentType("application/octet-stream");
    resp.setHeader("Content-Disposition", "attachment; filename=\"" + f.getName() + "\"");
    resp.setContentLengthLong(f.length());

    try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
         ServletOutputStream out = resp.getOutputStream()) {
        byte[] buf = new byte[4096];
        int r;
        while ((r = in.read(buf)) != -1) out.write(buf, 0, r);
    }
}

On the HTML side use a normal link to that servlet (progressive enhancement: download attribute helps modern browsers but IE does not support it). If you use Struts, return a stream result that sets the same headers.

Notes and cautions: never allow raw file paths from the query string (validate names or use an ID-to-path map to avoid traversal). Use application/octet-stream to force download when appropriate and set Content-Length. Some browser plugins or corporate proxies can still change behaviour, but the server header approach is the cross‑browser standard way to prompt Save/Save As.

Recommended Answers

All 6 Replies

I don't think you need php, but you will probably have to use some javascript or something if you want to force the link to be downloaded or saved.

You could use the target link attribute if you just need to stop it from opening in the same window as your current page.

I dont want to open it at all, what I want is the save as option should pop up. Like if some junk extension file you will give it will do.

File.asdf Link will give save as pop up
File1.doc or .txt will open the content of the file in the browser window.

Type the following magical incantation next to the link

"To save the file, right click the link and select Save as.."

Then the powers of darkeness and psychic forces extraordinary are invoked when the user does as he/she is told, and they can save the file.

Great reply drjohn, thanks for the same. But I am looking for some concrete solution. How we can pop up a window when user clicks on the link.

Well, a quick check using the keyword 'force download', indicates you can force downloads, but it isn't easy. The easiest way is to simply zip the file you want to have downloaded, since zipped files are always downloaded.

one way
<a href="pathto/savefile.php?file=pathtosomefile.txt">download somefile.txt</a>
savefile.php is below

<?php $file=$_GET['file'];
if(!$file) return false;
header("Content-disposition: attachment; filename=$file");
header('Content-type: text/plain;');
readfile("$file");
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.