can anybody tell me how do i create a link that forces a .doc file to download on the client computer or if that's really difficult to do without a ftp server..
then how can i display the file in browser as pdf

Dani AI

Generated

This thread asks how to force a Word document to download and how to display a file as a PDF in the browser. found that a plain link sometimes worked, and ’s mention of HTTP response headers is the right direction: to control download vs inline display reliably, set the MIME type and the Content-Disposition header on the server rather than relying on browser defaults.

A minimal Java/servlet pattern that forces a download (stream the file, set type and disposition) looks like this:

response.setContentType("application/msword");
response.setHeader("Content-Disposition", "attachment; filename=\"report.doc\"");
response.setContentLengthLong(file.length());

try (InputStream in = new FileInputStream(file);
     OutputStream out = response.getOutputStream()) {
    byte[] buf = new byte[8192];
    int len;
    while ((len = in.read(buf)) != -1) {
        out.write(buf, 0, len);
    }
}

For in-browser PDF viewing, serve the file with Content-Type: application/pdf and Content-Disposition: inline; filename="file.pdf". Many browsers expose a built-in PDF viewer when those headers are present; embedding with <embed>/<object> or using PDF.js gives more predictable UX across clients. Browser behavior can still vary, so explicit headers are the only reliable control (see the Content-Disposition and MIME type guidance linked below).

Troubleshooting tips: stream binary with getOutputStream() (avoid JSP writers), set Content-Length when possible, watch file permissions and server caching, and use the correct MIME for .docx (application/vnd.openxmlformats-officedocument.wordprocessingml.document). Filename encoding for non-ASCII characters requires special handling (RFC-style encoding). Converting Word to PDF on the server requires an extra tool (LibreOffice headless, a conversion service, or a commercial library) and is separate from header control.

References: Content-Disposition header and MIME types.

Recommended Answers

All 2 Replies

memegha123,
Did you work out on this issue? Do you know about HTTP response headers?

i figured out i just need to give the link n it downloads automatically...stupid of me i should have tried this earlier

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.