1

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.

1 Answers1

1

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'));
})()
Musa
  • 96,336
  • 17
  • 118
  • 137