D
D
Dmitry Tarasov2018-02-20 21:04:29
JavaScript
Dmitry Tarasov, 2018-02-20 21:04:29

How to do multi file upload using paste event js?

How to do multi file upload using paste event js ?
I want to make a multi-load image through the buffer, but it doesn't work, all the time it seems like one file gets into e.clipboardData.items. And

function pasteHandler(e) {
// если поддерживается event.clipboardData (Chrome)
    var filesArr = [];

    // console.log(e.clipboardData.getData("text/plain"));
    if (e.clipboardData) {
        // получаем все содержимое буфера
        var items = e.clipboardData.items;
        if (items) {
            // находим изображение
            console.log(e);
            for (var i = 0; i < items.length; i++) {
                console.log(i, items[i].type);
                if (items[i].type.indexOf("image") !== -1) {
                    var imgType = items[i].type.split('/')[1];

                    var isAccepted = acceptedImgTypes.some(function (item) {
                        return imgType === item;
                    });
                    if(!isAccepted){
                        var acceptedTypeString = '';
                        acceptedImgTypes.forEach(function (item,i) {
                            acceptedTypeString += item;
                            if(i !== acceptedImgTypes.length - 1) acceptedTypeString+=',';
                        });
                        alert('Такое расширение файла не поддерживается,только '+acceptedTypeString+", тип файла - "+imgType);
                        continue;
                    }

                    // представляем изображение в виде файла
                    var blob = items[i].getAsFile();
                    console.log(blob);
                    filesArr.push(blob);

                }
            }
            console.log(filesArr);
            downloadFileToServer(filesArr);
        }
        // для Firefox проверяем элемент с атрибутом contenteditable
    } else {
        setTimeout(checkInput, 1);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Arkhipov, 2018-04-24
@arvitaly

Apparently in chrome this API is not yet fully implemented. Here's one and two .
Everything works in Safari. Well, it's better not to use experimental functions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question