Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript-Edge browser- TypeError: Function expected [duplicate]

This code will show an error: TypeError: Function expected

function btnUploadImageEvent() {
    $(document).on("click", "#btn-upload-image", function () {

        // Iterate all td's in second column
        $("#table-image tbody tr:not(:first-child)").each(function (index, value) {
            var blob = value.cells[5].innerText;
            var fileName = (index + 1) + ".jpg";

            srcToFile(blob, fileName, "image/jpeg")
                .then(function (file) {
                    var formData = new FormData();
                    formData.append("file", file);

                    PassingBlobToServer(formData);
                }, function (error) {
                    MsgBox("Error", error, "error", "#upload-image-modal");
                }).then(function () {
                });
        });
    });
}

function srcToFile(src, fileName, mimeType) {
    return (fetch(src)
        .then(function (res) { return res.arrayBuffer(); })
        .then(function (buf) { return new File([buf], fileName, { type: mimeType }); })
    );
}

I read this article already: https://docs.microsoft.com/en-us/scripting/javascript/misc/function-expected

However, I'm still not understanding what MS say. This must be something wrong in my code. Please help me, thanks a lot.

like image 339
Nguyễn Minh Vương Avatar asked Apr 18 '18 03:04

Nguyễn Minh Vương


1 Answers

(As @Tan Duong's help.)

Problem is browser compatibility.

File() constructor is not supported by Edge or IE.

Workaround: we can use Blob constructor (supported) instead:

var blob = new Blob([blobContent], {type : 'image/jpeg'});

Refer: https://stackoverflow.com/a/43241922/9156186

like image 52
Nguyễn Minh Vương Avatar answered Oct 08 '22 12:10

Nguyễn Minh Vương