• 周四. 5月 30th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

给页面的图片加下载按钮

admin

11月 28, 2021
<img src="" /img>

<button onclick="handleDownloadClick">下载图片</button>
function handleDownloadClick() {
  var eleLink = document.createElement('a');
  eleLink.download = 你的文件名 + '.jpg';
  eleLink.style.display = 'none';

  var canvas = document.createElement('canvas');
  var context = canvas.getContext('2d');
  var width = domI  mg.naturalWidth;
  var height = domImg.naturalHeight;
  canvas.width = width;
  canvas.height = height;
  context.drawImage(domImg, 0, 0);

  eleLink.href = canvas.toDataURL('image/jpeg');
  document.body.appendChild(eleLink);
  eleLink.click();
  document.body.removeChild(eleLink);
}

但是,当 img 图片的 url 跨域的时候,会在新窗口打开,chrome 并不执行下载,

需要给 img 标签加一个属性:crossOrigin

<img crossOrigin="anonymous" src="" />
.signature { background-color: lightskyblue; padding: 10px; display: inline-flex; border: 1px dashed red }
.emphersize { font-weight: bold; color: darkblue }

作者:码路工人

公众号:码路工人有力量(Code-Power)

欢迎关注个人微信公众号 Coder-Power

一起学习提高吧~

《给页面的图片加下载按钮》有15个想法
  1. Sumatra Slim Belly Tonic takes pride in its manufacturing process, ensuring that every batch is produced in FDA-approved and GMP-certified facilities. This means that each and every bottle of Sumatra Slim Belly Tonic meets the highest standards of quality and safety. You can trust that you are getting a product that has undergone strict quality control measures and is backed by scientific research.

  2. Sugar Defender stands as a beacon of natural, side-effect-free blood sugar support. Crafted from a blend of pure, plant-based ingredients, this formula not only helps regulate blood sugar levels but also empowers you on your journey to weight loss, increased vitality, and overall life improvement.

  3. I have been struggling with this issue for a while and your post has provided me with much-needed guidance and clarity Thank you so much

  4. The ability of Alpilean to target both subcutaneous and visceral fat distinguishes it from other weight loss products. Subcutaneous fat is visible beneath the skin, whereas visceral fat is harmful fat that accumulates around the organs, increasing the risk of heart disease and other health problems. Alpilean ensures that you not only look better, but also feel better by attacking both types of fat.

  5. Sugar Defender orchestrates a reduction in blood sugar levels through multifaceted pathways. Its initial impact revolves around enhancing insulin sensitivity, optimizing the body’s efficient use of insulin, ultimately leading to a decrease in blood sugar levels. This proactive strategy works to prevent the storage of glucose as fat, mitigating the risk of developing type 2 diabetes.

  6. MetaNail is a topical formulation combining 20 organic and essential nutrients to repair, rejuvenate, and improve nail health. It can fight fungus and brittle or cracked nails. The serum is easy to apply and safer than most oral and topical nail products.

  7. Your posts always provide me with a new perspective and encourage me to look at things differently Thank you for broadening my horizons

  8. Thanks , I’ve just been looking for information about this subject for
    ages and yours is the best I have came upon so far. However,
    what about the conclusion? Are you positive in regards to the source?
    I saw similar here: Najlepszy sklep

  9. You can use parent management software to guide and supervise children’s behavior on the Internet. With the help of the following 10 smartest parent management software, you can track your child’s call history, browsing history, dangerous content access, apps they install, etc.

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注