I have example file paths on a web page like:
www.example.com/uploads/abc.pdf
www.example.com/uploads/abc.json
www.example.com/uploads/abc.txt
www.example.com/uploads/abc.avi
The file type can be anything. Some files are easily viewable in a browser like txt
or image files but some file types start downloading when opened in a browser.
How can I know whether the URL file path is viewable in div or downloadable only? Is there any way to decide from the MIME type or anything we can check with Javascript/jQuery if that file is viewable in a div or downloadable only before viewing/downloading starts?
Any help would be appreciated.
CodePudding user response:
Try with jQuery:
jQuery.get(url, function (response, status, xhr) {
if (xhr.getResponseHeader("content-type").indexOf("text") > -1)
// url content is of type : text
else
// url has downloadable content
});
For more intel, refer to : https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/getResponseHeader