M
M
Moshiwa2021-07-06 18:29:59
JavaScript
Moshiwa, 2021-07-06 18:29:59

How to put a temporary file using js in input type=file?

Implemented sound recording on the site, after the recording is stopped, the file is saved to temporary storage. How to send this file to the form? I tried to send ajax directly to the server, but this is not quite what I need.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nadim Zakirov, 2021-07-12
@Moshiwa

Suppose your sound file is in the blob variable, then you can insert it into the field like this:

// Создаем коллекцию:
var dt  = new DataTransfer();
dt.items.add(new File([blob], 'FileName.wav', {type: blob.type}));
var file_list = dt.files;

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

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

If your audio format is not .wav , replace the file name with the correct one.

A
Alexey Yarkov, 2021-07-06
@yarkov

I tried to send ajax directly to the server, but this is not quite what I need

This is exactly what is needed. As you want - does not work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question