1. //文件转base64
    2. function toDataURL(url) {
    3. fetch(url)
    4. .then(response => response.blob())
    5. .then(blob => new Promise((resolve, reject) => {
    6. const reader = new FileReader()
    7. reader.onloadend = () => resolve(reader.result)
    8. reader.onerror = reject
    9. reader.readAsDataURL(blob)
    10. }));
    11. }
    12. //base64转File对象
    13. function base64ToFileObj(filePath, fileName) {
    14. toDataURL(filePath)
    15. .then(dataUrl => {
    16. console.log('RESULT:', dataUrl.length)
    17. //base64文本
    18. const url = dataUrl;
    19. //base64转file对象
    20. fetch(url)
    21. .then(res => res.blob())
    22. .then(blob => {
    23. const file = new File([blob], fileName, {
    24. type: "image/png"
    25. })
    26. //最终输出的文件对象
    27. console.log('fileObj', file);
    28. });
    29. })
    30. }