Jak przekonwertować Blob na plik w JavaScript
Muszę wgrać obrazek na serwer NodeJS do jakiegoś katalogu. Używam do tego modułu connect-busboy
node.
Miałem dataURL
obrazek, który przekonwertowałem na blob używając następującego kodu:
dataURLToBlob: function(dataURL) {
var BASE64_MARKER = ';base64,';
if (dataURL.indexOf(BASE64_MARKER) == -1) {
var parts = dataURL.split(',');
var contentType = parts[0].split(':')[1];
var raw = decodeURIComponent(parts[1]);
return new Blob([raw], {type: contentType});
}
var parts = dataURL.split(BASE64_MARKER);
var contentType = parts[0].split(':')[1];
var raw = window.atob(parts[1]);
var rawLength = raw.length;
var uInt8Array = new Uint8Array(rawLength);
for (var i = 0; i < rawLength; ++i) {
uInt8Array[i] = raw.charCodeAt(i);
}
return new Blob([uInt8Array], {type: contentType});
}
Używam AngularJS po stronie klienta. Mam już napisany kod do przesłania pliku/obrazu, więc potrzebuję sposobu na przekonwertowanie Bloba na plik, aby przesłać obraz.
Czy ktoś mógłby mi z tym pomóc?4 answers
Ta funkcja zamienia {[3] } na File
i działa świetnie dla mnie.
Vanilla JavaScript
function blobToFile(theBlob, fileName){
//A Blob() is almost a File() - it's just missing the two properties below which we will add
theBlob.lastModifiedDate = new Date();
theBlob.name = fileName;
return theBlob;
}
TypeScript (z odpowiednimi typami)
public blobToFile = (theBlob: Blob, fileName:string): File => {
var b: any = theBlob;
//A Blob() is almost a File() - it's just missing the two properties below which we will add
b.lastModifiedDate = new Date();
b.name = fileName;
//Cast to a File() type
return <File>theBlob;
}
Użycie
var myBlob = new Blob();
//do stuff here to give the blob some data...
var myFile = blobToFile(myBlob, "my-image.png");
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-19 13:06:46
Możesz użyć konstruktora Plików:
var file = new File([myBlob], "name");
Zgodnie ze specyfikacją W3 spowoduje to dodanie bajtów, które obiekt blob zawiera do bajtów nowego obiektu File i utworzenie pliku o podanej nazwie http://www.w3.org/TR/FileAPI/#dfn-file
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-02 09:53:58
Mój nowoczesny wariant:
function blob2file(blobData) {
const fd = new FormData();
fd.set('a', blobData);
return fd.get('a');
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-13 09:56:03
Użyj saveAs
Na FileSaver.js Projekt github.
FileSaver.js
implementuje interfejs saveAs()
FileSaver w przeglądarkach, które nie obsługują go natywnie.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-14 10:40:55