最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to detect heic image before upload in JavaScript on iOS11? - Stack Overflow

programmeradmin3浏览0评论

We have an image uploading application and we press images on client side using an external JavaScript library before uploading.

Now we have came across an issue specific to iOS11 when uploading image files having the extension of .heif or .heic.

As an example when we submit the form with abc.heic image, client side browser application crashes when trying to press it through above library. So we want to restrict attaching .heic images to the image input.

Unfortunately we cannot identify (I think so) .heic images on server side, because we receive file name as abc.jpgon backend since iOS11 automatically convert .heic images into jpg images before uploading.

Therefore I want to detect it on client side, but when I check file MIME type on client side using below code, it returns image/jpg (actually I don't have an iPhone, so some other friend did it for me). So it looks like .heic image is already converted to jpg internally by iOS11 before I check MIME type. (But for some reason, those converted jpg files are not able to press by this library)

$(document).on('change', '[id=uploading_file]', (evt) => {
    'use strict'
    const files = [...evt.target.files];
    var uploadingFile = files[0];
    var fileType = uploadingFile["type"]; // get file MIME type
    var ValidImageTypes = ["image/jpg", "image/jpeg", "image/png", "image/gif", "image/bmp"];

    if (jQuery.inArray(fileType, ValidImageTypes) >= 0) {
        // image pression code goes here.
    }
});

Some alternative solution:

I did some other alternative fix also to avoid attaching .heic images as below though it is not a guaranteed solution. Changed accept="image/*" to accept="image/jpg, image/jpeg, image/png, image/gif, image/bmp" , but it still shows abc image on file chooser.

We have an image uploading application and we press images on client side using an external JavaScript library before uploading.

Now we have came across an issue specific to iOS11 when uploading image files having the extension of .heif or .heic.

As an example when we submit the form with abc.heic image, client side browser application crashes when trying to press it through above library. So we want to restrict attaching .heic images to the image input.

Unfortunately we cannot identify (I think so) .heic images on server side, because we receive file name as abc.jpgon backend since iOS11 automatically convert .heic images into jpg images before uploading.

Therefore I want to detect it on client side, but when I check file MIME type on client side using below code, it returns image/jpg (actually I don't have an iPhone, so some other friend did it for me). So it looks like .heic image is already converted to jpg internally by iOS11 before I check MIME type. (But for some reason, those converted jpg files are not able to press by this library)

$(document).on('change', '[id=uploading_file]', (evt) => {
    'use strict'
    const files = [...evt.target.files];
    var uploadingFile = files[0];
    var fileType = uploadingFile["type"]; // get file MIME type
    var ValidImageTypes = ["image/jpg", "image/jpeg", "image/png", "image/gif", "image/bmp"];

    if (jQuery.inArray(fileType, ValidImageTypes) >= 0) {
        // image pression code goes here.
    }
});

Some alternative solution:

I did some other alternative fix also to avoid attaching .heic images as below though it is not a guaranteed solution. Changed accept="image/*" to accept="image/jpg, image/jpeg, image/png, image/gif, image/bmp" , but it still shows abc image on file chooser.

Share Improve this question edited Dec 22, 2018 at 9:45 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Jun 1, 2018 at 5:50 S.BasnagodaS.Basnagoda 7212 gold badges8 silver badges22 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

It's not as robust as using the mime type, but you can use the heic and/or heif extension to restrict which files can be selected.

For instance:

<input type="file" accept=".heic">

发布评论

评论列表(0)

  1. 暂无评论