如何将字符串转换为从 FileReader.readAsDataURL() 生成的实际文件

How to convert a String to actual file that was generated from FileReader.readAsDataURL()

我使用这个生成了一个文件字符串:

const reader = new window.FileReader();
reader.readAsDataURL(file);
reader.onloadend = () => {
  var x = reader.result.toString();
  console.log("File String :: ", x);
};

如何将此字符串转换为实际文件,如果之前的文件是 PDF,则此字符串将转换为 pdf 文件

reader.result.toString() 给我这样的字符串 "data:application/pdf;base64,JVBERi0xLjQKJSDi48/..........."

我想把它转换回 pdf

FileReader.readAsDataURL()

如果您有一个 base64 字符串,请使用 atob to decode it and then use the Blob() 构造函数来获取 Blob 对象(File 的超级构造函数)

//strip off the data uri prefix
let encodedString = dataUrlString.replace('data:application/pdf;base64,','')
let data = atob(encodedString);
let blob = new Blob([data],{'type':'optional mime type here'});

//if you need a literal File object
let file = new File(blob,"filename.pdf",{type:"optional mime type"});