Search code examples
htmlmime-typesdata-uri

Is it possible to use common MIME-type for Data URI?


For example:

data:image/*;base64,R0lGODlhEAAQAMQAAORHHOVSKudf

I worry about cases when an actual MIME-type will be not as expected, such as for data:image/png I will get data:image/jpg etc.


Solution

  • This data URI is not valid.

    Neither the docs nor the RFC precise anything about a possible /* as media subtype, nor any default subtypes, so you'll have to write a full media type.

    That is indeed understandable: if a program knows that a file contains an image but don't know whether it's encoded in .jpg, .png or something else, it won't be able to open it. Similarly, browsers won't be able to understand a base-64-encoded image without a subtype parameter.

    Depending on your use case, you might wanna use this library to infer content type from the raw data directly.