別のイメージから切り取ったbase64イメージからファイルオブジェクトを作成しようとしています。私はそれを行うことができますが、結果のファイルサイズは実際のサイズのほぼ3倍です。私が使用している機能は以下の通りです。JavaScriptのbase64イメージをファイルオブジェクトに変換する
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;
}
ファイルサイズが非常に大きくなる理由は何ですか?どうすれば圧縮できますか?前もって感謝します。
で@Endlessによって回答を参照してください、
data URI
のBlob
表現を作成し、取得するためにfetch()
を利用することができます_結果の 'Blob'のサイズが違うとあなたはどのように判断しましたか? '.name'と' .lastModifiedDate'を設定したいのであれば、なぜ 'File'コンストラクタを使わないのですか? 'File'は' Blob'から継承し、 'Blob'は' File'から継承しません。 – guest271314渡された 'data URI'の内容よりも大きい' .size'を持つ結果のBlobを再現できません。注意すべき点は、結果として得られる 'Blob'は' data URI'と同じデータ内容ではありません。 \t _ "結果のファイルサイズは実際のサイズのほぼ3倍です" _を示すにはstacksnippetsまたはplnkr https://plnkr.coを作成できますか? https://stackoverflow.com/help/mvceを参照してください。 – guest271314