• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I set response.setHeader to not start downloading automatically  RSS feed

 
Jitender Mandhaniya
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I set response.setHeader so that it will not start download the files if the files are not supported to display in the browser

code.  
JSP file to use the servlet that dispay the content in the iframe






DownloadServlet.java





From the above code i am able to view pdf, png audio, video, but some unsupported files like docx, zip, rar are start downloading. so what should i edit in the code so that it will not start downloading.
And my audio or video files get start play on loading page, they should not be start automatically.
Thank You
sorry for my weak english. 
 
Paul Clapham
Sheriff
Posts: 22526
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's two problems with that. First of all your servlet will have no way of knowing whether the user's browser is configured to display files of a particular MIME type or not. And second, what do you want to do instead of "starting to download automatically"? The only other option I can think of is to return something other than 200 as the response code, but you'd have to find a suitable code which can explain why you aren't going to download the file.
 
Jitender Mandhaniya
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to show error message if the browser is not able to display the file.
 
Paul Clapham
Sheriff
Posts: 22526
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So for example you would like code which prohibits users from downloading, say, ZIP files? Wouldn't it be easier to just not have zip files available for download?
 
Ron McLeod
Saloon Keeper
Posts: 1601
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jitender Mandhaniya wrote:I want to show error message if the browser is not able to display the file.

As Paul already said, there really is no way for the server to know what the rendering capabilities are of the browser.  The browser knows, and there is a mechanism for the browser to inform the server, but typically browsers tell the server they support everything.

The HTTP Accept header presented by the browser should detail the types of content it will accept (and be able to render), and if the information provided by the browser was actually accurate, the server could use this information to decide what type of content to return.  But unfortunately, browsers usually include a wildcard of */*, indicating that they will accept all content types, so the server really cannot make an informed decision.

Here an example of a request from my Chrome browser:You can see that the Accept header includes */*


Specialized clients, like web service clients are usually a lot better at really indicating what they can use:
 
Paul Clapham
Sheriff
Posts: 22526
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Browsers will always "accept" PDF files, as far as I know. But whether they will accept a PDF file and then display it or whether they will accept it and just store it somewhere temporarily, or ask the user where it should be stored permanently, only the browser knows for sure.

I have to say I don't understand the reason behind this requirement. If you're designing a server then it's really none of your business what the user does with a downloaded file.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!