如何在JavaScript中将Blob转换为文件

如何在JavaScript中将Blob转换为文件,第1张

如何在JavaScript中将Blob转换为文件

此函数将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;}

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;}

用法

var myBlob = new Blob();//do stuff here to give the blob some data...var myFile = blobToFile(myBlob, "my-image.png");


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5566460.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存