ski*_*kip 81 javascript blob file-upload node.js
我需要将图像上传到NodeJS服务器到某个目录.我正在使用connect-busboy节点模块.
我dataURL使用以下代码将我转换为blob的图像:
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});
}
Run Code Online (Sandbox Code Playgroud)
我需要一种方法将blob转换为文件以上传图像.
有人可以帮我吗?
小智 147
您可以使用File构造函数:
var file = new File([myBlob], "name");
Run Code Online (Sandbox Code Playgroud)
根据w3规范,这将将blob包含的字节附加到新File对象的字节,并创建具有指定名称的文件 http://www.w3.org/TR/FileAPI/#dfn-file
CBa*_*arr 122
此函数将a转换Blob为a File,它对我来说非常有用.
香草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;
}
Run Code Online (Sandbox Code Playgroud)
TypeScript(正确的打字)
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;
}
Run Code Online (Sandbox Code Playgroud)
用法
var myBlob = new Blob();
//do stuff here to give the blob some data...
var myFile = blobToFile(myBlob, "my-image.png");
Run Code Online (Sandbox Code Playgroud)
Ale*_*urt 22
为了让我工作,我必须明确提供类型,尽管它包含在 blob 中:
const file = new File([blob], 'untitled', { type: blob.type })
Run Code Online (Sandbox Code Playgroud)
Hos*_*hef 21
打字稿
public blobToFile = (theBlob: Blob, fileName:string): File => {
return new File([theBlob], fileName, { lastModified: new Date().getTime(), type: theBlob.type })
}
Run Code Online (Sandbox Code Playgroud)
Javascript
function blobToFile(theBlob, fileName){
return new File([theBlob], fileName, { lastModified: new Date().getTime(), type: theBlob.type })
}
Run Code Online (Sandbox Code Playgroud)
输出
File {name: "fileName", lastModified: 1597081051454, lastModifiedDate: Mon Aug 10 2020 19:37:31 GMT+0200 (Eastern European Standard Time), webkitRelativePath: "", size: 601887, …}
lastModified: 1597081051454
lastModifiedDate: Mon Aug 10 2020 19:37:31 GMT+0200 (Eastern European Standard Time) {}
name: "fileName"
size: 601887
type: "image/png"
webkitRelativePath: ""
__proto__: File
Run Code Online (Sandbox Code Playgroud)
mil*_*ili 15
约书亚·尼克松(Joshua P Nixon)的回答是正确的,但我也必须设定最后修改日期。所以这是代码。
var file = new File([blob], "file_name", {lastModified: 1534584790000});
Run Code Online (Sandbox Code Playgroud)
1534584790000是Unix时间戳“ GMT:星期六,2018年8月18日上午九时33分10秒 ”
我的现代变体:
function blob2file(blobData) {
const fd = new FormData();
fd.set('a', blobData);
return fd.get('a');
}
Run Code Online (Sandbox Code Playgroud)
var blob = new Blob(["Hello, world!"], { type: "text/plain;charset=utf-8" });
var file = new File([blob], "name.txt");
Run Code Online (Sandbox Code Playgroud)
现在您可以上传为 .txt 文件