3

I have this page where a list of many files are there like

 a.jpg
 b.png
 c.php
 d.js
 e.pdf
 f.doc

They are all links and user can click on them and they are opened in a new tab. The problem is when a user clicks on JPG file it opens but when user clicks on DOC file instead of opening it prompts to download. How can i stop the download and open a blank page instead. Did a lot of searching but found nothing.

Ace
  • 841
  • 9
  • 23
  • 1
    How you expect the browser to display the (possibly Word) document? – bagonyi Dec 04 '13 at 12:59
  • Which browser does support Microsoft Office files to be displayed? It all depends on the header `disposition` automatically sent by your server depending on the Mime-Type. When you send Doc files to the browser you'll get binary code displayed. – Daniel W. Dec 04 '13 at 13:00
  • @bagonyi thats the reson i want to show a blank page instead of opening or downloading files like doc, docx etc – Ace Dec 04 '13 at 13:00
  • If you want a blank page and no download, simply don't allow download instead and remove the link or remove the files or move them to a different directory. – Daniel W. Dec 04 '13 at 13:01
  • @DanFromGermany I am not trying to open Doc file. If i remove the link i'll have to do for a lot of files. In some browsers PDF opens and in some it prompts to download. Removing link will stop this. Is there a solution : **if it can be opened in browser open and if not show a blank page** – Ace Dec 04 '13 at 13:03
  • The oposite to your question might help :: force+file+download+instead+of+opening+in+browser :: https://www.google.ca/search?newwindow=1&site=&source=hp&q=force+file+download+instead+of+opening+in+browser AND http://stackoverflow.com/questions/19257238/force-browser-to-open-file-instead-of-prompting-download AND http://stackoverflow.com/questions/16124064/force-browsers-to-download-a-file-rather-than-open – Milche Patern Dec 04 '13 at 13:05
  • 1
    I think the downvoter didn't understand the question. – GolezTrol Dec 04 '13 at 13:09

1 Answers1

4

You cannot tell which capabilities the users browser has and what actions are linked to which filetype.

Simple example: The server does not know weather you have installed Microsoft Word and the server doesn't know either if your setting is "open in browser" or "always download this type of file". It would be a security problem if we can tell all this from the server. Because JavaScript is able to interact between client and server, it does also not know about all this.

Daniel W.
  • 31,164
  • 13
  • 93
  • 151
  • means there is no way of doing this? – Ace Dec 04 '13 at 13:12
  • 2
    I think this is the right answer. You can force a browser to always download a file, but not vice versa. The server cannot tell whether the browser will download or display a file. (The server doesn't know what a browser *is*.) And in Javascript I don't think you can tell how a link will be interpreted by a browser. A link is just a link, and you don't know what is behind it in the first place. You can make an url that ends in `.png` and still return a doc file from it. – GolezTrol Dec 04 '13 at 13:13
  • @GolezTrol I have found out that files with mime type **Image, audio , video** can be opened in browser and those with **application** may or may not be opened. So would it be ok to check mime and open the file using jquery? – Ace Dec 04 '13 at 13:23
  • 1
    image/bmp or image/tiff will also not be opened by your browser. And some browsers might display `application` too (for instance application/xml). And some browsers can display certain files depending on plug-ins or configuration. For instance IE can actually open Word Documents by embedding Word when displaying the page. In the same way Adobe Reader is used to display PDF. Besides, you've got to do an actual request to learn the mime type. Are you going to download all those documents just to find out which links to hide? What is the use case? – GolezTrol Dec 04 '13 at 13:26
  • @GolezTrol that would be a problem . I'm going for a popup to display only images on open request. tnx for ur guidance :) – Ace Dec 04 '13 at 13:29