I am working on developing a http client. In the get request, I am displaying whatever the data is contained in HTTP response, even if its a downloadable file. Now I am planning to add a check to it if the Http response contains a file then instead of displaying the data, save the data in a file. The problem is I am unable to determine the right condition to find if the response is a downloadable file. Initially i thought that it is determined using content-type, if the content-type is application/something then its a file. But when I tried with developing a local web page which hosted a file and set the content-type to a random string with "/" anywhere the file was downloaded successfully( I am using chrome). Please help in determining the criteria for a http response to represent a file.
Thanks for replying Paul. I can not rely on content-disposition as many web sites does not add this header to the response. In my case also I am not adding content-disposition and yet the browser is downloading the file.
Sure, you can't rely on it being present. But that doesn't mean you should ignore it, either, if it is present. If you get a Content-Disposition header then you ought to at least consider doing what it says to do.
As for how browsers decide what to do with responses, you could certainly try to mimic that if you thought it was a good thing to do. But now you're talking about the design of your HTTP client rather than its implementation. There's no magic method which tells you whether it's possible to display a file with a particular MIME type; you have to build up your own cross-reference between MIME types and executable programs on each system. Consider for example that you can't display a PDF unless you have something on the computer which is capable of displaying PDFs.
Joined: Aug 30, 2011
Exactly, I am trying to mimic the working of the browser but I am not associating any mime type with an application. i am just trying to download the file. Like if I send a get request to URL which have a pdf/iso/image/doc/exe/zip file etc, the client will simply save the file to a predefined location and display only text or xml files i.e files which are human readable/editable in notepad.
subject: How to determine if the Http response is a File which needs to be downloaded