N
N
Nadim Zakirov2020-02-03 11:24:38
JavaScript
Nadim Zakirov, 2020-02-03 11:24:38

How to unlock FileList for writing?

I understand that the collection of FileList files is read-only and cannot be edited, but in fact in our world everything is arranged in such a way that if something is impossible, but you really want to, then you can. The only question is HOW?

Perhaps there are builds of Chrome with disabled security options? Or something third?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nadim Zakirov, 2020-11-17
@zkrvndm

Life hack to create a collection of FileList files :

var info = 'Какой-нибудь текст';

var dt  = new DataTransfer();
dt.items.add(new File([info], 'primer.txt', {type: 'text/plain'}));
var file_list = dt.files;

console.log('Коллекция файлов создана:');
console.dir(file_list);

// Вставим созданную коллекцию в реальное поле:
document.querySelector('input[type="file"]').files = file_list;

Instead of text, you can also put Blob into the info variable - this is a feature of the new File() function, which can take as the first parameter both an array of texts and an array of Blob . In fact, the life hack above allows us to download any file from our site with JavaScript and paste it into absolutely any field - which is simply extremely useful for browser bots. Also, the method above can be applied to edit files already selected by the user. For example, if you need to remove certain files from the file selection field and at the same time do it not fictitiously, but really. Many thanks to Mmx Symfony user
, who dug up this solution somewhere in the depths of foreign sites. I myself, at one time, no matter how much I searched, everywhere there was an answer that it was impossible, but it turned out that nothing like that =)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question