8 examples of 'convert blob to file' in JavaScript

Every line of 'convert blob to file' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
76export function file2Blob(file) {
77 return new Promise((resolve, reject) => {
78 var reader = new FileReader()
79 reader.readAsArrayBuffer(file)
80
81 reader.onload = function(e) {
82 resolve(new Blob([reader.result], { type: file.type }))
83 }
84
85 reader.onerror = function(err) {
86 reject(err)
87 }
88 })
89}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

14function filenameForBlob( blob ) {
15 if ( blob.getName() ) {
16 return blob.getName()
17 }
18
19 const contentType = blob.getContentType()
20 if ( contentTypeToExtension[ contentType ] ) {
21 return DEFAULT_FILENAME + '.' + contentTypeToExtension[ contentType ]
22 }
23
24 throw new Error( 'Unsupported content type: ' + contentType )
25}
90function uploadFile(blobFile) {
91 var fd = new FormData();
92
93 fd.append(fileKey, blobFile, fileName);
94 for (var prop in params) {
95 if(params.hasOwnProperty(prop)) {
96 fd.append(prop, params[prop]);
97 }
98 }
99
100 xhr[fileKey].open("POST", server);
101 xhr[fileKey].onload = function(evt) {
102 if (xhr[fileKey].status === 200) {
103 var result = new FileUploadResult();
104 result.bytesSent = file.size;
105 result.responseCode = xhr[fileKey].status;
106 result.response = xhr[fileKey].response;
107 delete xhr[fileKey];
108 onSuccess(result);
109 } else if (xhr[fileKey].status === 404) {
110 onFail(new FileTransferError(FileTransferError.INVALID_URL_ERR, server, filePath, xhr[fileKey].status, xhr[fileKey].response));
111 } else {
112 onFail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, xhr[fileKey].status, xhr[fileKey].response));
113 }
114 };
115 xhr[fileKey].ontimeout = function(evt) {
116 onFail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, xhr[fileKey].status, xhr[fileKey].response));
117 };
118 xhr[fileKey].onerror = function () {
119 onFail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, this.status, xhr[fileKey].response));
120 };
121 xhr[fileKey].upload.onprogress = function (evt) {
122 if (evt.loaded > 0) {
123 onSuccess(evt);
124 }
125 };
126
127 for (var header in headers) {
128 if (headers.hasOwnProperty(header)) {
129 xhr[fileKey].setRequestHeader(header, headers[header]);
130 }
131 }
132
133 requestAnimationFrame(function () {
134 xhr[fileKey].send(fd);
135 });
136}
68function uploadFile(blobFile) {
69 var fd = new FormData();
70
71 fd.append(fileKey, blobFile, fileName);
72 for (var prop in params) {
73 if(params.hasOwnProperty(prop)) {
74 fd.append(prop, params[prop]);
75 }
76 }
77
78 xhr = new XMLHttpRequest();
79 xhr.open("POST", server);
80 xhr.onload = function(evt) {
81 if (xhr.status === 200) {
82 var result = new FileUploadResult();
83 result.bytesSent = file.size;
84 result.responseCode = xhr.status;
85 result.response = xhr.response;
86 win(result);
87 } else if (xhr.status === 404) {
88 fail(new FileTransferError(FileTransferError.INVALID_URL_ERR, server, filePath, xhr.status, xhr.response));
89 } else {
90 fail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, xhr.status, xhr.response));
91 }
92 };
93 xhr.ontimeout = function(evt) {
94 fail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, xhr.status, xhr.response));
95 };
96 xhr.onerror = function () {
97 fail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, this.status, xhr.response));
98 };
99 xhr.onprogress = function (evt) {
100 win(evt);
101 };
102
103 for (var header in headers) {
104 if (headers.hasOwnProperty(header)) {
105 xhr.setRequestHeader(header, headers[header]);
106 }
107 }
108
109 xhr.send(fd);
110}
33async function mkBlobFile (blobRoot, ...pathPart) {
34 pathPart.length > 1 && await mkBlobDir(blobRoot, ...pathPart.slice(0, pathPart.length - 1))
35 let fullPath = path.join(blobRoot, ...pathPart)
36 return writeFile(fullPath, data)
37}
13export function blobToImage(blob) {
14 return new Promise((resolve, reject) => {
15 let url = URL.createObjectURL(blob);
16 let image = new Image();
17
18 image.onload = () => {
19 canvas.width = image.width;
20 canvas.height = image.height;
21
22 ctx.drawImage(image, 0, 0);
23
24 resolve(image);
25 };
26
27 image.onerror = (e) => {
28 reject(e);
29 };
30
31 image.src = url;
32 });
33}
117export function blob2ObjectURL(blobOrFile) {
118 return URL && URL.createObjectURL
119 ? URL.createObjectURL(blobOrFile)
120 : new Error('blob2ObjectURL转换出错')
121}
72export async function blobToDataUrl(blob) {
73 return new Promise((resolve, reject) => {
74 const reader = new FileReader();
75 reader.onload = event => {
76 resolve(event.target.result);
77 };
78 reader.readAsDataURL(blob);
79 });
80}

Related snippets