Joignez un blob à une entrée du fichier type sous une forme

Comment ajouter blob à la saisie du type de fichier

// Input of type file <input type="file" name="uploadedFile" id="uploadedFile" accept="image/*"><br> 
 // I am getting image from webcam and converting it to a blob function takepicture() { canvas.width = width; canvas.height = height; canvas.getContext('2d').drawImage(video, 0, 1, width, height); var data = canvas.toDataURL('image/png'); var dataURL = canvas.toDataURL(); var blob = dataURItoBlob(dataURL); photo.setAttribute('src', data); } function dataURItoBlob(dataURI) { var binary = atob(dataURI.split(',')[1]); var array = []; for(var i = 0; i < binary.length; i++) { array.push(binary.charCodeAt(i)); return new Blob([new Uint8Array(array)], {type: 'image/jpeg'}); } // How can I append this var blob to "uploadedFile". I want to add this on form submit 

J'ai eu un problème similaire avec une forme assez complexe dans une application angulaire, donc, au lieu de la forme, je viens d'envoyer le blob individuellement en utilisant XMLHttpRequest() . Cette "blob" particulière a été créée dans un contexte WebAudioAPI , créant une piste audio dans le navigateur de l'utilisateur.

 var xhr = new XMLHttpRequest(); xhr.open('POST', 'someURLForTheUpload', true); //my url had the ID of the item that the blob corresponded to xhr.responseType = 'Blob'; xhr.setRequestHeader("x-csrf-token",csrf); //if you are doing CSRF stuff xhr.onload = function(e) { /*irrelevant code*/ }; xhr.send(blob); 

Vous ne pouvez pas modifier l'entrée du file mais vous pouvez utiliser une entrée hidden pour transmettre les données. ex.:

 var hidden_elem = document.getElementById("hidden"); hidden_elem.value = blob;