1
我想从另一个图像中剪切的base64图像创建一个文件对象。我能够做到这一点,但生成的文件大小几乎是实际大小的三倍。以下是我正在使用的功能:将base64图像转换为JavaScript中的文件对象
convertDataURItoFile(dataURI, fileName) {
// convert base64 to raw binary data held in a string
// doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
var byteString = atob(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
// write the bytes of the string to an ArrayBuffer
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
// write the ArrayBuffer to a blob, and you're done
var blob: any = new Blob([ia], { type: mimeString });
//A Blob() is almost a File() - it's just missing the two properties below which we will add
blob.lastModifiedDate = new Date();
blob.name = fileName;
//Cast to a File() type
return <File>blob;
}
有关为什么文件大小急剧增加的任何想法?我如何压缩它?提前致谢。
_“生成的文件大小几乎是三倍的实际尺寸”来创建并获得
data URI
Blob
表示,看到答案_你如何确定产生的“Blob”的大小是不同的大小?如果你想设置'.name'和'.lastModifiedDate'为什么你不使用'File'构造函数? 'File'从'Blob'继承,'Blob'不从'File'继承。 – guest271314无法重现产生的'.size'大于'data URI'内容的Blob。虽然注意,结果'Blob'与传递'data URI'的数据内容不同。你能创建一个stacksnippets或plnkr https://plnkr.co来演示\t _“生成的文件大小几乎是实际大小的三倍”_?请参阅https://stackoverflow.com/help/mvce – guest271314