1. fetch('flowers.jpg').then(function(response) {
    2. if(response.ok) {
    3. return response.blob();
    4. }
    5. throw new Error('Network response was not ok.');
    6. }).then(function(myBlob) {
    7. var objectURL = URL.createObjectURL(myBlob);
    8. myImage.src = objectURL;
    9. downloadFileByBlob(objectURL, 'flowers.jpg');
    10. }).catch(function(error) {
    11. console.log('There has been a problem with your fetch operation: ', error.message);
    12. });
    13. function downloadFileByBlob(blobUrl, filename) {
    14. const eleLink = document.createElement('a');
    15. eleLink.download = filename;
    16. eleLink.style.display = 'none';
    17. eleLink.href = blobUrl;
    18. // 触发点击
    19. document.body.appendChild(eleLink);
    20. eleLink.click();
    21. // 然后移除
    22. document.body.removeChild(eleLink);
    23. }