Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attach a blob to an input of type file in a form

How to append blob to input of type file?

<!-- 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

like image 717
Vicky Avatar asked May 07 '14 07:05

Vicky


People also ask

What is file type BLOB?

BLOB stands for a “Binary Large Object,” a data type that stores binary data. Binary Large Objects (BLOBs) can be complex files like images or videos, unlike other data strings that only store letters and numbers. A BLOB will hold multimedia objects to add to a database; however, not all databases support BLOB storage.

What is BLOB in source code?

Blob URLs contain pseudo protocols that can create a temporary URL to audio and video files. This type of URL essentially acts as a fake source for the media on the website, so you can't download it directly.

How do you use a BLOB?

Using blobs To construct a Blob from other non-blob objects and data, use the Blob() constructor. To create a blob that contains a subset of another blob's data, use the slice() method. To obtain a Blob object for a file on the user's file system, see the File documentation.

Is a file object a BLOB?

A File object is a specific kind of Blob , and can be used in any context that a Blob can. In particular, FileReader , URL. createObjectURL() , createImageBitmap() , and XMLHttpRequest.


4 Answers

It is possible to set value of <input type="file">.

To do this you create File object from blob and new DataTransfer object:

let file = new File([data], "img.jpg",{type:"image/jpeg", lastModified:new Date().getTime()});
let container = new DataTransfer();

Then you add file to container thus populating its 'files' property, which can be assigned to 'files' property of file input:

container.items.add(file);
fileInputElement.files = container.files;

Here is a fiddle with output, showing that file is correctly placed into input. The file is also passed correctly to server on form submit. This works at least on Chrome 88.

If you need to pass multiple files to input you can just call container.items.add multiple times. So you can add files to input by keeping track of them separately and overwriting its 'files' property as long as this input contains only generated files (meaning not selected by user). This can be useful for image preprocessing, generating complex files from several simple ones (e.g. pdf from several images), etc.

API references:

  • File object
  • DataTransfer object
like image 81
Kartearis Avatar answered Oct 06 '22 19:10

Kartearis


I had a similar problem with a fairly complex form in an angular app, so instead of the form I just sent the blob individually using XMLHttpRequest(). This particular "blob" was created in a WebAudioAPI context, creating an audio track in the user's browser.

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);
like image 28
MikeSmithDev Avatar answered Oct 06 '22 19:10

MikeSmithDev


You can't change the file input but you can use a hidden input to pass data. ex.:

var hidden_elem = document.getElementById("hidden");
hidden_elem.value = blob;
like image 44
Stubbies Avatar answered Oct 06 '22 17:10

Stubbies


I had take too much time to find how to do "url, blob to input -> preview" so I had do a example you can check here https://stackoverflow.com/a/70485949/6443916 https://vulieumang.github.io/vuhocjs/file2input-input2file/

image bonus enter image description here

like image 20
Đinh Tiến Vũ Avatar answered Oct 06 '22 17:10

Đinh Tiến Vũ