Search code examples
javascriptblobmime-typesfilereaderipfs

How to find MIME type of IPFS (no extension given) using Javascript?


If I have an IPFS CID address (with no extension), of a file like this for instance https://ipfs.infura.io/ipfs/QmNhq4hx1KfTw1a5pTtudGXm9Q4xhWdeuWtPSSG19SSZeU , how can I find its MIME type using Javascript?

I've tried the following:

let file='https://ipfs.infura.io/ipfs/QmNhq4hx1KfTw1a5pTtudGXm9Q4xhWdeuWtPSSG19SSZeU'
if (window.FileReader && window.Blob) {
    console.log("All the File APIs are supported by the browser.");
    console.log("Type: " + file.type);
} else {
    console.log("File and Blob are not supported by the browser");
}

This is always giving me undefined. Any help would be much appreciated.


Solution

  • You have a url, so to get the mime type you can make a HTTP HEAD request.

    (async function(){ 
    let file='https://ipfs.infura.io/ipfs/QmNhq4hx1KfTw1a5pTtudGXm9Q4xhWdeuWtPSSG19SSZeU';
    var req = await fetch(file, {method:'HEAD'});
    console.log(req.headers.get('content-type'));
    })()