How to determine MIME type of copy-pasted image clipboard?

雨燕双飞 提交于 2021-02-18 21:59:07

问题


Can I assume that Mac OS X clipboard image data is png?

When I try to MIME-detect the clipboard data, it returns application/octet.

This has the undesirable effect of causing every browser to download the image rather than display it.

If I force the content-type to image/png, everything seems fine, but I wondered if there is a way for me to not have to make the assumption?

$log.debug(e.originalEvent.clipboardData);
for (var i = 0; i < e.originalEvent.clipboardData.items.length; i++) {
var item = e.originalEvent.clipboardData.items[i];
$log.info("Item type: " ,item);
if (item.type.indexOf("image") != -1) {
    $scope.token.images = [];
    $log.debug(item.getAsFile(), {});
...

I use https://github.com/broofa/node-mime to detect MIME types.


回答1:


At least in theory, no, you can't assume that.

Here is what apple says: "To get a list of types currently available on the clipboard, you can use the types property of the clipboardData object. This property contains an array of strings with the MIME types of the available data." Webkit DOM Programming Topics

At least according to w3c the options are:

  • image/png
  • image/jpg
  • image/jpeg
  • image/gif
  • image/svg+xml

edit:

The reason the MIME-detect returns application/octet is that the library you are using falls back to that when it doesn't know the type as you can see in this line of the test.js file:

assert.equal('fallback', mime.charsets.lookup('application/octet-stream', 'fallback'));

This answer explains in more detail how to deal with this "arbitrary binary data."



来源:https://stackoverflow.com/questions/31790587/how-to-determine-mime-type-of-copy-pasted-image-clipboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!